def addItemType(self, session, itemTypeName, description, domainName): entityDisplayName = self._getEntityDisplayName(ItemType) self.logger.debug('Adding %s %s' % (entityDisplayName, itemTypeName)) dbDomain = self.domainHandler.findDomainByName(session, domainName) try: self.getItemTypeByName(session, itemTypeName, dbDomain.id) raise ObjectAlreadyExists( '%s %s for domain %s already exists.' % (entityDisplayName, itemTypeName, dbDomain.name)) except ObjectNotFound as ex: # ok pass # Create Entity Db Object dbItemType = ItemType(name=itemTypeName) if description: dbItemType.description = description dbItemType.domain = dbDomain session.add(dbItemType) session.flush() self.logger.debug('Inserted %s id %s' % (entityDisplayName, dbItemType.id)) return dbItemType
def addUser(self, session, username, firstName, lastName, middleName, email, description, password): self.logger.debug('Adding user %s' % username) try: dbUserInfo = session.query(UserInfo).filter( UserInfo.username == username).one() raise ObjectAlreadyExists('User %s already exists.' % (username)) except NoResultFound as ex: # ok pass # Create user dbUserInfo = UserInfo(username=username, first_name=firstName, last_name=lastName) if middleName: dbUserInfo.middle_name = middleName if email: dbUserInfo.email = email if password: dbUserInfo.password = password if description: dbUserInfo.description = description session.add(dbUserInfo) session.flush() self.logger.debug('Inserted user id %s' % dbUserInfo.id) return dbUserInfo
def addGroup(self, session, groupName, description): self.logger.debug('Adding group %s' % groupName) try: dbGroupInfo = session.query(UserGroup).filter( UserGroup.name == groupName).one() raise ObjectAlreadyExists('Group %s already exists.' % (groupName)) except NoResultFound, ex: # ok pass
def addItem(self, session, domainName, name, derivedFromItemId, itemIdentifier1, itemIdentifier2, entityTypeNames, qrId, description, createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime=None, lastModifiedOnDateTime=None): dbItems = session.query(Item).filter(Item.name == name).all() for item in dbItems: if item.item_identifier1 == itemIdentifier1 \ and item.item_identifier2 == itemIdentifier2 \ and item.domain.name == domainName \ and item.derived_from_item_id == derivedFromItemId: raise ObjectAlreadyExists('Item with name %s already exists.' % name) # Create entity info entityInfoArgs = (createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime, lastModifiedOnDateTime) # Create item dbItem = Item(name=name) dbItem.domain = self.domainHandler.findDomainByName( session, domainName) dbItem.derived_from_item_id = derivedFromItemId dbItem.qr_id = qrId dbItem.item_identifier1 = itemIdentifier1 dbItem.item_identifier2 = itemIdentifier2 session.add(dbItem) session.flush() # Add self element self.addItemElement(session, None, dbItem.id, None, False, description, *entityInfoArgs) self.logger.debug('Inserted item id %s' % dbItem.id) if entityTypeNames is not None: if type(entityTypeNames) is list: for entityTypeName in entityTypeNames: self.addItemEntityType(session, None, entityTypeName, dbItem) elif type(entityTypeNames) is str: self.addItemEntityType(session, None, entityTypeNames, dbItem) return dbItem
def addItemElementProperty(self, session, itemElementId, propertyTypeName, tag=None, value=None, units=None, description=None, enteredByUserId=None, isUserWriteable=None, isDynamic=None, displayValue=None, targetValue=None, enteredOnDateTime=None, allowInternal=False): dbItemElement = self.getItemElementById(session, itemElementId) dbPropertyType = self.propertyTypeHandler.getPropertyTypeByName( session, propertyTypeName) dbItemElementProperties = self.propertyValueHandler.getItemElementProperties( session, itemElementId, propertyTypeName) # Verify that we are not adding the same property again for dbItemElementProperty in dbItemElementProperties: dbPropertyValue = dbItemElementProperty.propertyValue if dbPropertyValue.tag == tag and dbPropertyValue.value == value and dbPropertyValue.units == units and dbPropertyValue.description == description: raise ObjectAlreadyExists( 'There is already identical property of type %s for item element id %s.' % (propertyTypeName, itemElementId)) self.permissionHandler.verifyPermissionsForWriteToItemElement( session, enteredByUserId, dbItemElementObject=dbItemElement) dbPropertyValue = self.propertyValueHandler.createPropertyValue( session, propertyTypeName, tag, value, units, description, enteredByUserId, isUserWriteable, isDynamic, displayValue, targetValue, enteredOnDateTime, allowInternal) session.add(dbPropertyValue) dbItemElementProperty = ItemElementProperty() dbItemElementProperty.itemElement = dbItemElement dbItemElementProperty.propertyValue = dbPropertyValue entityInfo = dbItemElement.entityInfo dbItemElement.entityInfo = self.entityInfoHandler.updateEntityInfo( session, entityInfo, enteredByUserId) session.add(dbItemElement) session.add(dbItemElementProperty) session.flush() self.logger.debug( 'Added property value (type: %s) for item element id %s' % (propertyTypeName, itemElementId)) return dbItemElementProperty
def addSource(self, session, sourceName, description, contactInfo, url): self.logger.debug('Adding Source %s' % sourceName) try: dbSource = session.query(Source).filter( Source.name == sourceName).one() raise ObjectAlreadyExists('Source %s already exists.' % (sourceName)) except NoResultFound, ex: # ok pass
def addUser(self, session, username, firstName, lastName, middleName, email, description, password): self.logger.debug('Adding user %s' % username) try: dbUserInfo = session.query(UserInfo).filter( UserInfo.username == username).one() raise ObjectAlreadyExists('User %s already exists.' % (username)) except NoResultFound, ex: # ok pass
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 _prepareAddUniqueNameObj(self, session, entityDbObject, name): entityDisplayName = self._getEntityDisplayName(entityDbObject) self.logger.debug('Adding %s %s' % (entityDisplayName, name)) try: session.query(entityDbObject).filter( entityDbObject.name == name).one() raise ObjectAlreadyExists('%s %s already exists.' % (entityDisplayName, name)) except NoResultFound as ex: # ok pass
def addPropertyValueMetadata(self, session, propertyValueId, metadataKey, metadataValue, userId): entityDisplayName = self._getEntityDisplayName(PropertyMetadata) try: self.getPropertyValueMetadataByKey(session, propertyValueId, metadataKey) raise ObjectAlreadyExists( '%s with key %s for property id %s already exists.' % (entityDisplayName, metadataKey, propertyValueId)) except ObjectNotFound, ex: # ok pass
def addItemType(self, session, itemTypeName, description, domainName): entityDisplayName = self._getEntityDisplayName(ItemType) self.logger.debug('Adding %s %s' % (entityDisplayName, itemTypeName)) dbDomain = self.domainHandler.findDomainByName(session, domainName) try: self.getItemTypeByName(session, itemTypeName, dbDomain.id) raise ObjectAlreadyExists( '%s %s for domain %s already exists.' % (entityDisplayName, itemTypeName, dbDomain.name)) except ObjectNotFound, ex: # ok pass
def addItemElement(self, session, name, parentItemId, containedItemId, isRequired, description, createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime=None, lastModifiedOnDateTime=None): 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) parentSelfElement = self.getSelfElementByItemId(session, parentItemId) self.verifyPermissionsForWriteToItemElement( session, createdByUserId, dbItemElementObject=parentSelfElement) # Create entity info dbEntityInfo = self.entityInfoHandler.createEntityInfo( session, createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable, createdOnDataTime, lastModifiedOnDateTime) # Create item dbItemElement = ItemElement(name=name, description=description) dbItemElement.entityInfo = dbEntityInfo if parentItemId: dbItemElement.parentItem = self.getItemById(session, parentItemId) if containedItemId: dbItemElement.containedItem = self.getItemById( session, containedItemId) entityInfo = parentSelfElement.entityInfo parentSelfElement.entityInfo = self.entityInfoHandler.updateEntityInfo( session, entityInfo, createdByUserId) session.add(parentSelfElement) session.add(dbItemElement) session.flush() self.logger.debug('Inserted item Element id %s' % dbItemElement.id) return dbItemElement
def addUserToGroup(self, session, username, groupName): dbUserInfo = self.findUserInfoByUsername(session, username) userGroupHandler = UserGroupHandler() dbUserGroup = userGroupHandler.findUserGroupByName(session, groupName) dbUserGroups = session.query(UserGroup).join(UserUserGroup).filter( and_(UserUserGroup.user_id == dbUserInfo.id, UserUserGroup.user_group_id == UserGroup.id)).all() for g in dbUserGroups: if g.id == dbUserGroup.id: raise ObjectAlreadyExists( 'User %s is already a member of group %s.' % (username, groupName)) dbUserUserGroup = UserUserGroup(user_id=dbUserInfo.id, user_group_id=dbUserGroup.id) session.add(dbUserUserGroup) session.flush() self.logger.debug('Added user %s to group %s' % (username, groupName)) return dbUserUserGroup
def addGroup(self, session, groupName, description): self.logger.debug('Adding group %s' % groupName) try: dbGroupInfo = session.query(UserGroup).filter( UserGroup.name == groupName).one() raise ObjectAlreadyExists('Group %s already exists.' % (groupName)) except NoResultFound as ex: # ok pass # Create group dbGroupInfo = UserGroup(name=groupName) if description: dbGroupInfo.description = description session.add(dbGroupInfo) session.flush() self.logger.debug('Inserted group id %s' % dbGroupInfo.id) return dbGroupInfo
def addSource(self, session, sourceName, description, contactInfo, url): self.logger.debug('Adding Source %s' % sourceName) try: dbSource = session.query(Source).filter( Source.name == sourceName).one() raise ObjectAlreadyExists('Source %s already exists.' % (sourceName)) except NoResultFound as ex: # ok pass # Create source Handler dbSource = Source(name=sourceName) if description: dbSource.description = description if contactInfo: dbSource.contactInfo = contactInfo if url: dbSource.url = url session.add(dbSource) session.flush() self.logger.debug('Inserted source id %s' % dbSource.id) return dbSource
def addValidItemElementRelationship(self, session, firstItemElementId, secondItemElementId, relationshipTypeName, enteredByUserId, relationshipDetails=None, description=None): # defaults firstItemConnectorId = None secondItemConnectorId = None linkItemElementId = None resourceTypeName = None label = None mayAdd = False existingItemElementRelationship = None relationshipType = self.relationshipTypeHandler.getRelationshipTypeByName( session, relationshipTypeName) relationshipTypeName = relationshipType.name firstItemElement = self.getItemElementById(session, firstItemElementId) secondItemElement = self.getItemElementById(session, secondItemElementId) firstDomainName = firstItemElement.parentItem.domain.name secondDomainName = secondItemElement.parentItem.domain.name ierList = self.getItemElementRelationshipListByRelationshipTypeNameAndFirstItemElementId( session, relationshipTypeName, firstItemElementId) if relationshipTypeName == self.relationshipTypeHandler.LOCATION_RELATIONSHIP_TYPE_NAME: if firstDomainName == self.domainHandler.INVENTORY_DOMAIN_NAME and secondDomainName == self.domainHandler.LOCATION_DOMAIN_NAME: if ierList.__len__() > 0: # Only one is allowed update if ierList.__len__() > 1: raise InvalidObjectState( "Item has multiple location relationships.") locationRelationship = ierList[0] if locationRelationship.second_item_element_id == secondItemElementId: raise InvalidObjectState( "Item is already in the specified location") existingItemElementRelationship = locationRelationship mayAdd = True else: mayAdd = True else: raise InvalidArgument( "First item element should be inventory and second location. Invalid item element ids provided." ) elif relationshipTypeName == self.relationshipTypeHandler.MAARC_CONNECTION_RELATIONSHIP_TYPE_NAME: if firstDomainName == self.domainHandler.INVENTORY_DOMAIN_NAME or firstDomainName == self.domainHandler.MACHINE_DESIGN_NAME: if secondDomainName == self.domainHandler.MAARC_DOMAIN_NAME: # Check for duplicates for itemElementRelationship in ierList: if itemElementRelationship.first_item_element_id == firstItemElementId and itemElementRelationship.second_item_element_id == secondItemElementId: raise ObjectAlreadyExists( "The maarc connection relationship between the specified item elements already exists" ) mayAdd = True if not mayAdd: raise InvalidArgument( "First item element should be inventory or machine design and second maarc. Invalid item element ids provided." ) else: raise InvalidArgument( "Unsupported relationship type name has been specified: %s." % relationshipTypeName) if mayAdd: dbItemElementRelationship = self.addItemElementRelationship( session, firstItemElementId, secondItemElementId, firstItemConnectorId, secondItemConnectorId, linkItemElementId, relationshipTypeName, relationshipDetails, resourceTypeName, label, description, existingItemElementRelationship) # Add initial history item self.addItemElementRelationshipHistory( session, dbItemElementRelationship.id, firstItemElementId, secondItemElementId, firstItemConnectorId, secondItemConnectorId, linkItemElementId, relationshipDetails, resourceTypeName, label, description, enteredByUserId) return dbItemElementRelationship
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)