def __init__(self, **kwargs): self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) for col, value in kwargs.items(): if col not in self.columns: raise DbError('Column %s not found in table %s' % (col, self.__class__.__name__)) else: setattr(self, col, value)
def addItemElement(self, session, name, parentItemId, containedItemId, isRequired, description, createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime=None, lastModifiedOnDateTime=None, selfElementCreation=False): dbItemElements = session.query(ItemElement).filter( ItemElement.name == name).all() for element in dbItemElements: if element.parent_item_id == parentItemId and element.name == name: raise ObjectAlreadyExists( 'Item Element with name %s already exists.' % name) if not selfElementCreation: parentSelfElement = self.getSelfElementByItemId( session, parentItemId) self.permissionHandler.verifyPermissionsForWriteToItemElement( session, createdByUserId, dbItemElementObject=parentSelfElement) entityInfo = parentSelfElement.entityInfo parentSelfElement.entityInfo = self.entityInfoHandler.updateEntityInfo( session, entityInfo, createdByUserId) session.add(parentSelfElement) # Create entity info dbEntityInfo = self.entityInfoHandler.createEntityInfo( session, createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime, lastModifiedOnDateTime) # Create item dbItemElement = ItemElement(name=name, description=description, is_required=isRequired) dbItemElement.entityInfo = dbEntityInfo if parentItemId: dbItemElement.parentItem = self.getItemById(session, parentItemId) if containedItemId: dbItemElement.containedItem = self.getItemById( session, containedItemId) try: session.add(dbItemElement) session.flush() except OperationalError, err: raise DbError(err.message)
def updateItemElement(self, session, itemElementId, lastModifiedUserId, containedItemId=-1, isRequired=-1, name=None, description=None, ownerUserId=None, ownerGroupId=None, isGroupWriteable=None): dbItemElement = self.getItemElementById(session, itemElementId) self.permissionHandler.verifyPermissionsForWriteToItemElement( session, lastModifiedUserId, dbItemElementObject=dbItemElement) self.entityInfoHandler.updateEntityInfo(session, dbItemElement.entityInfo, lastModifiedUserId, ownerUserId, ownerGroupId, isGroupWriteable) if containedItemId != -1: if containedItemId is None: dbItemElement.containedItem = None else: dbItemElement.containedItem = self.getItemById( session, containedItemId) if name is not None: dbItemElement.name = name if isRequired != -1: dbItemElement.is_required = isRequired if description is not None: dbItemElement.description = description try: session.add(dbItemElement) session.flush() except OperationalError, err: raise DbError(err.message)
def addItem(self, session, domainName, name, createdByUserId, ownerUserId, ownerGroupId, itemProjectName, itemIdentifier1=None, itemIdentifier2=None, qrId=None, description=None, isGroupWriteable=True, createdOnDataTime=None, lastModifiedOnDateTime=None, derivedFromItemId=None, entityTypeNames=None): # Create entity info entityInfoArgs = (createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime, lastModifiedOnDateTime) domain = self.domainHandler.findDomainByName(session, domainName) # Verify valid project if itemProjectName is None: raise InvalidArgument( "Please specify an item project for the new item") self.getItemProjectByName(session, itemProjectName) try: if self.getItemByUniqueAttributes(session, domain.id, name, itemIdentifier1, itemIdentifier2, derivedFromItemId): raise ObjectAlreadyExists( "Item with attributes already exists: " "(domain=%s, name='%s', item_identifier1=%s, item_identifier2=%s and derivedFromItemId=%s)" % (domain.name, name, itemIdentifier1, itemIdentifier2, derivedFromItemId)) except ObjectNotFound: pass if (itemDomain.INVENTORY_DOMAIN_NAME == domainName): if derivedFromItemId is None: raise InvalidArgument( "To create an inventory item, the derived from item id must be provided." ) else: derivedItem = self.getItemById(session, derivedFromItemId) if not derivedItem.domain.name == itemDomain.CATALOG_DOMAIN_NAME: raise InvalidArgument( "To create an inventory item, the derived from item id must be of a catalog item." ) # Create item dbItem = Item(name=name) dbItem.domain = domain dbItem.derived_from_item_id = derivedFromItemId dbItem.qr_id = qrId dbItem.item_identifier1 = itemIdentifier1 dbItem.item_identifier2 = itemIdentifier2 try: session.add(dbItem) session.flush() except OperationalError, err: raise DbError(err.message)