class ItemElementControllerImpl(CdbObjectManager):

    def __init__(self):
        CdbObjectManager.__init__(self)
        self.itemDbApi = ItemDbApi()

    def getItemElementById(self, itemElementId):
        return self.itemDbApi.getItemElementById(itemElementId)

    def addItemElement(self, parentItemId, name, createdByUserId, ownerUserId, ownerGroupId,
       containedItemId=None, description=None, isRequired=None, isGroupWriteable=True):
        return self.itemDbApi.addItemElement(name, parentItemId, containedItemId, isRequired, description,
                                      createdByUserId, ownerUserId, ownerGroupId, isGroupWriteable)

    def updateItemElement(self, itemElementId, lastModifiedUserId, containedItemId=-1, isRequired=-1,
                          name=None, description=None, ownerUserId=None, ownerGroupId=None, isGroupWriteable=None):
        return self.itemDbApi.updateItemElement(itemElementId, lastModifiedUserId, containedItemId,
                                                           isRequired, name, description, ownerUserId, ownerGroupId,
                                                           isGroupWriteable)

    def addPropertyValueForItemElementWithId(self, itemElementId, propertyTypeName, enteredByUserId,
                                      tag=None, value=None, units=None, description=None,
                                      isUserWriteable=None, isDynamic=False, displayValue=None):
        propertyValueAdded = self.itemDbApi.addItemElementProperty(itemElementId, propertyTypeName,
                                                                   tag, value, units, description,
                                                                   enteredByUserId, isUserWriteable, isDynamic, displayValue=displayValue)

        return propertyValueAdded
Example #2
0
 def __init__(self):
     self.itemDbApi = ItemDbApi()
     self.userDbApi = UserDbApi()
     self.propertyDbApi = PropertyDbApi()
     self.logDbApi = LogDbApi()
     self.emailUtility = CdbEmailUtility.getInstance()
     self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance()
     self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
Example #3
0
    def runCommand(self):
        self.parseArgs(usage="""
    get-item-projects

Description:
    Get all registered Item Projects.
        """)
        api = ItemDbApi()
        projects = api.getItemProjects()
        for project in projects:
            print project.getDisplayString(self.getDisplayKeys(),
                                           self.getDisplayFormat())
    def runCommand(self):
        self.parseArgs(usage="""
    add-item-project --name=PROJECTPNAME
        [--description=DESCRIPTION]
Description:
    Adds an Item Project into CDB database. This command goes directly to the
    database and must be run from a CDB administrator account.
        """)
        self.checkArgs()
        api = ItemDbApi()
        name = self.getName()
        description = self.getDescription()
        info = api.addItemProject(name, description)
        print info.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
class ItemElementControllerImpl(CdbObjectManager):
    def __init__(self):
        CdbObjectManager.__init__(self)
        self.itemDbApi = ItemDbApi()

    def getItemElementById(self, itemElementId):
        return self.itemDbApi.getItemElementById(itemElementId)
Example #6
0
class SparePartsTask:

    ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME = "Component Instance Status"
    ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE = "Spare"

    SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME = 'Spare Parts Configuration'
    SPARE_PARTS_CONFIGURATION_MIN_KEY = 'minQuantity'
    SPARE_PARTS_CONFIGURATION_EMAIL_KEY = 'email'
    SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE = 'None'
    SPARE_PARTS_WARNING_LOG_LEVEL = 'Spares Warning'
    CDB_SYSTEM_ACCOUNT_USERNAME = '******'
    CATALOG_DOMAIN_NAME = 'Catalog'
    SPARE_PARTS_EMAIL_NOTIFICATION_NAME = 'Spare Parts'

    def __init__(self):
        self.itemDbApi = ItemDbApi()
        self.userDbApi = UserDbApi()
        self.propertyDbApi = PropertyDbApi()
        self.logDbApi = LogDbApi()
        self.emailUtility = CdbEmailUtility.getInstance()
        self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance()
        self.logger = LoggingManager.getInstance().getLogger(
            self.__class__.__name__)

    def getPropertyMetadataDict(self, propertyValue):
        propertyValueId = propertyValue.data['id']
        propertyMetadataList = self.propertyDbApi.getPropertyMetadataForPropertyValueId(
            propertyValueId)
        propertyMetadataDict = {}
        for propertyMetadata in propertyMetadataList:
            key = propertyMetadata.data['metadataKey']
            value = propertyMetadata.data['metadataValue']
            propertyMetadataDict[key] = value
        return propertyMetadataDict

    def getOwnerUserEmail(self, itemId):
        selfItemElement = self.itemDbApi.getSelfElementByItemId(itemId)
        entityInfo = selfItemElement.data['entityInfo']
        ownerUserInfo = entityInfo['ownerUserInfo']
        return ownerUserInfo.data['email']

    def getSystemAccountUserId(self):
        cdbUser = self.userDbApi.getUserByUsername(
            self.CDB_SYSTEM_ACCOUNT_USERNAME)
        return cdbUser.data['id']

    def getSelfElementIdForItemId(self, itemId):
        selfItemElement = self.itemDbApi.getSelfElementByItemId(itemId)
        return selfItemElement.data['id']

    def addSparePartsWarningLogEntryToItem(self, itemElementId, message):
        systemAccountUserId = self.getSystemAccountUserId()
        self.itemDbApi.addItemElementLog(itemElementId, message,
                                         systemAccountUserId, None, None, None,
                                         None,
                                         self.SPARE_PARTS_WARNING_LOG_LEVEL)

    @staticmethod
    def generateSparesMessage(minSpares, currentSpares):
        if currentSpares == 1:
            plural = ''
        else:
            plural = 's'
        return "Item has %s spare part%s & requires %s" % (currentSpares,
                                                           plural, minSpares)

    def checkSpares(self):
        self.logger.debug('Checking status of spare parts.')
        catalogItemsWithSparePartsConfiguration = self.itemDbApi.getItemsWithPropertyType(
            self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME,
            itemDomainName=self.CATALOG_DOMAIN_NAME)

        for catalogItem in catalogItemsWithSparePartsConfiguration:
            catalogItemId = catalogItem.data['id']
            catalogSelfElementId = self.getSelfElementIdForItemId(
                catalogItemId)

            sparePartsConfigurationPropertyValue = self.propertyDbApi.getPropertyValueListForItemElementId(
                catalogSelfElementId,
                propertyTypeName=self.
                SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if sparePartsConfigurationPropertyValue:
                metadataDict = self.getPropertyMetadataDict(
                    sparePartsConfigurationPropertyValue[0])
            else:
                raise ObjectNotFound(
                    "Could not find required property: %s" %
                    self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if self.SPARE_PARTS_CONFIGURATION_MIN_KEY in metadataDict:
                minSpares = int(
                    metadataDict[self.SPARE_PARTS_CONFIGURATION_MIN_KEY])
            else:
                raise CdbException(
                    "required metadata %s not specified for spare part configuration"
                    % self.SPARE_PARTS_CONFIGURATION_MIN_KEY)

            email = ''
            if metadataDict.has_key(self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY):
                emailValue = metadataDict[
                    self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY]
                if emailValue == self.SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE:
                    email = None
                else:
                    email = emailValue
            else:
                # Use owner user email.
                email = self.getOwnerUserEmail(catalogItemId)

            sparePartsList = self.itemDbApi.getItemsWithPropertyType(
                self.ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME,
                itemDerivedFromItemId=catalogItemId,
                propertyValueMatch=self.
                ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE)
            spares = sparePartsList.__len__()

            if minSpares > spares:
                validNotification = True
                sparesMessage = self.generateSparesMessage(minSpares, spares)

                spareLogEntries = self.logDbApi.getLogEntriesForItemElementId(
                    catalogSelfElementId, self.SPARE_PARTS_WARNING_LOG_LEVEL)
                if spareLogEntries:
                    lastSpareLogEntry = spareLogEntries[-1]
                    lastSparesLogMessage = lastSpareLogEntry.data['text']
                    if lastSparesLogMessage == sparesMessage:
                        validNotification = False

                if validNotification:
                    self.addSparePartsWarningLogEntryToItem(
                        catalogSelfElementId, sparesMessage)
                    if email is not None:
                        itemUrl = self.cdbPortalUrlUtility.getItemUrlAddress(
                            catalogItemId)
                        catalogItemName = catalogItem.data['name']
                        catalogItemModel = catalogItem.data['item_identifier1']
                        catalogItemAlternateName = catalogItem.data[
                            'item_identifier2']
                        itemDictValue = '<a href=%s>%s</a>' % (itemUrl,
                                                               catalogItemName)

                        # Create an ordered dict for the table emailed to user.
                        informationDict = OrderedDict()
                        informationDict['Name'] = itemDictValue
                        informationDict['Model'] = catalogItemModel
                        informationDict[
                            'Alternate Name'] = catalogItemAlternateName
                        informationDict['Spares On Hand'] = spares
                        informationDict['Minimum Spares Required '] = minSpares

                        emailTable = CdbEmailUtility.generateSimpleHtmlTableMessage(
                            informationDict)

                        emailMessage = '%s <br/><br/> %s' % (sparesMessage,
                                                             emailTable)

                        self.emailUtility.sendEmailNotification(
                            email, self.SPARE_PARTS_EMAIL_NOTIFICATION_NAME,
                            emailMessage)
 def __init__(self):
     CdbObjectManager.__init__(self)
     self.itemDbApi = ItemDbApi()
     self.logControllerImpl = LogControllerImpl()
     self.propertyDbApi = PropertyDbApi()
     self.storageUtility = StorageUtility.getInstance()
class ItemControllerImpl(CdbObjectManager):

    CATALOG_ITEM_DOMAIN_NAME = "Catalog"
    INVENTORY_ITEM_DOMAIN_NAME = "Inventory"
    LOCATION_ITEM_DOMAIN_NAME = "LOCATION"

    def __init__(self):
        CdbObjectManager.__init__(self)
        self.itemDbApi = ItemDbApi()
        self.logControllerImpl = LogControllerImpl()
        self.propertyDbApi = PropertyDbApi()
        self.storageUtility = StorageUtility.getInstance()

    def getItemById(self, itemId):
        return self.itemDbApi.getItemById(itemId)

    def getItemByQrId(self, itemQrId):
        return self.itemDbApi.getItemByQrId(itemQrId)

    def getFirstItemRelationship(self, itemId, relationshipTypeName):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        return self.itemDbApi.getFirstItemElementRelationshipList(
            relationshipTypeName, selfElementId)

    def addItemElementRelationshipByQrId(self,
                                         firstItemQrId,
                                         secondItemQrId,
                                         relationshipTypeName,
                                         enteredByUserId,
                                         relationshipDetails=None,
                                         description=None):
        firstItemId = self.itemDbApi.getItemByQrId(firstItemQrId).data['id']
        secondItemId = self.itemDbApi.getItemByQrId(secondItemQrId).data['id']

        return self.__addItemElementRelationship(firstItemId, secondItemId,
                                                 relationshipTypeName,
                                                 enteredByUserId,
                                                 relationshipDetails,
                                                 description)

    def addItemElementRelationship(self,
                                   firstItemId,
                                   secondItemId,
                                   relationshipTypeName,
                                   enteredByUserId,
                                   relationshipDetails=None,
                                   description=None):
        return self.__addItemElementRelationship(firstItemId, secondItemId,
                                                 relationshipTypeName,
                                                 enteredByUserId,
                                                 relationshipDetails,
                                                 description)

    def __addItemElementRelationship(self,
                                     firstItemId,
                                     secondItemId,
                                     relationshipTypeName,
                                     enteredByUserId,
                                     relationshipDetails=None,
                                     description=None):
        firstSelfElement = self.itemDbApi.getSelfElementByItemId(firstItemId)
        firstSelfElementId = firstSelfElement.data['id']
        secondSelfElement = self.itemDbApi.getSelfElementByItemId(secondItemId)
        secondSelfElementId = secondSelfElement.data['id']

        return self.itemDbApi.addValidItemElementRelationship(
            firstSelfElementId, secondSelfElementId, relationshipTypeName,
            enteredByUserId, relationshipDetails, description)

    def getItemByUniqueAttributes(self,
                                  domainName,
                                  itemName,
                                  itemIdentifier1=None,
                                  itemIdentifier2=None,
                                  derivedFromItemId=None):
        domain = self.itemDbApi.getDomainByName(domainName)
        return self.itemDbApi.getItemByUniqueAttributes(
            domain.data['id'], itemName, itemIdentifier1, itemIdentifier2,
            derivedFromItemId)

    def addLogEntryForItemWithQrId(self, qrId, logEntryText, enteredByUserId,
                                   attachmentName, cherryPyData):
        item = self.itemDbApi.getItemByQrId(qrId)

        itemId = item.data['id']
        return self.addLogEntryForItemWithItemId(itemId, logEntryText,
                                                 enteredByUserId,
                                                 attachmentName, cherryPyData)

    def addLogEntryForItemWithItemId(self, itemId, logEntryText,
                                     enteredByUserId, attachmentName,
                                     cherryPyData):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        # Add log entry
        itemElementLog = self.itemDbApi.addItemElementLog(
            selfElementId, logEntryText, enteredByUserId)
        logEntry = itemElementLog.data['log']

        # Check if log has an attachment that needs to be stored
        if attachmentName is not None and len(attachmentName) > 0:
            logId = logEntry.data['id']
            logAttachment = self.logControllerImpl.addLogAttachment(
                logId, attachmentName, None, enteredByUserId, cherryPyData)
            del (logAttachment.data['log'])
            logAttachmentJsonRep = logAttachment.getFullJsonRep()
            logEntry.data['logAttachmentAdded'] = logAttachmentJsonRep

        return logEntry

    def addPropertyImageToItem(self, itemId, imageFileName, enteredByUserId,
                               cherryPyData):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        if self.itemDbApi.verifyPermissionsForWriteToItemElement(
                enteredByUserId, selfElementId):
            storedAttachmentName = self.storageUtility.storePropertyImage(
                cherryPyData, imageFileName)

            propertyValue = self.itemDbApi.addItemElementImageProperty(
                selfElementId, enteredByUserId, storedAttachmentName,
                imageFileName)

            return propertyValue

        return None

    def addPropertyValueForItemWithId(self,
                                      itemId,
                                      propertyTypeName,
                                      enteredByUserId,
                                      tag=None,
                                      value=None,
                                      units=None,
                                      description=None,
                                      isUserWriteable=None,
                                      isDynamic=False,
                                      displayValue=None):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        propertyValueAdded = self.itemDbApi.addItemElementProperty(
            selfElementId,
            propertyTypeName,
            tag,
            value,
            units,
            description,
            enteredByUserId,
            isUserWriteable,
            isDynamic,
            displayValue=displayValue)

        return propertyValueAdded

    def deletePropertyValuesFromItemByItemId(self, itemId, propertyTypeName,
                                             enteredByUserId):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']
        return self.itemDbApi.deleteItemElementProperties(
            selfElementId, propertyTypeName, enteredByUserId)

    def getParentItems(self, itemId):
        return self.itemDbApi.getParentItems(itemId)

    def getItemElementsForItem(self, itemId):
        return self.itemDbApi.getItemElementsByItemId(itemId)

    def getLogEntriesForItemWithQrId(self, qrId):
        item = self.itemDbApi.getItemByQrId(qrId)
        itemId = item.data['id']
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        return self.logControllerImpl.getLogEntriesForItemElement(
            selfElementId)

    def getLogEntriesForItemWithId(self, itemId):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']
        return self.logControllerImpl.getLogEntriesForItemElement(
            selfElementId)

    def getCatalogItems(self):
        return self.itemDbApi.getItemsOfDomain(self.CATALOG_ITEM_DOMAIN_NAME)

    def getLocationItems(self):
        return self.itemDbApi.getItemsOfDomain(self.LOCATION_ITEM_DOMAIN_NAME)

    def getLocationItemsWithoutParents(self):
        return self.itemDbApi.getItemsOfDomainWithoutParents(
            self.LOCATION_ITEM_DOMAIN_NAME)

    def getItemsDerivedFromItemId(self, derivedFromItemId):
        return self.itemDbApi.getItemsDerivedFromItem(derivedFromItemId)

    def getPropertiesForItemId(self, itemId):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        return self.propertyDbApi.getPropertyValueListForItemElementId(
            selfElementId)

    def getDomains(self):
        return self.itemDbApi.getDomains()

    def addItem(self,
                domainName,
                name,
                createdByUserId,
                ownerUserId,
                ownerGroupId,
                itemProjectName=None,
                itemIdentifier1=None,
                itemIdentifier2=None,
                qrId=None,
                description=None,
                isGroupWriteable=True,
                entityTypeNames=None,
                derivedFromItemId=None):
        return self.itemDbApi.addItem(domainName=domainName,
                                      name=name,
                                      createdByUserId=createdByUserId,
                                      ownerUserId=ownerUserId,
                                      ownerGroupId=ownerGroupId,
                                      itemProjectName=itemProjectName,
                                      itemIdentifier1=itemIdentifier1,
                                      itemIdentifier2=itemIdentifier2,
                                      qrId=qrId,
                                      description=description,
                                      isGroupWriteable=isGroupWriteable,
                                      entityTypeNames=entityTypeNames,
                                      derivedFromItemId=derivedFromItemId)

    def getAvailableInventoryItemStatuses(self):
        return self.itemDbApi.getAvailableInventoryItemStatuses()

    def getInventoryItemStatus(self, itemId):
        return self.itemDbApi.getInventoryItemStatus(itemId)

    def updateInventoryItemStatus(self, itemId, statusName, enteredByUserId):
        return self.itemDbApi.updateInventoryItemStatus(
            itemId, statusName, enteredByUserId)
 def __init__(self):
     CdbObjectManager.__init__(self)
     self.itemDbApi = ItemDbApi()
Example #10
0
 def __init__(self):
     CdbObjectManager.__init__(self)
     self.itemDbApi = ItemDbApi()
     self.logControllerImpl = LogControllerImpl()
     self.propertyDbApi = PropertyDbApi()
Example #11
0
class ItemControllerImpl(CdbObjectManager):

    CATALOG_ITEM_DOMAIN_NAME = "Catalog"
    INVENTORY_ITEM_DOMAIN_NAME = "Inventory"
    LOCATION_ITEM_DOMAIN_NAME = "LOCATION"

    def __init__(self):
        CdbObjectManager.__init__(self)
        self.itemDbApi = ItemDbApi()
        self.logControllerImpl = LogControllerImpl()
        self.propertyDbApi = PropertyDbApi()

    def getItemById(self, itemId):
        return self.itemDbApi.getItemById(itemId)

    def addLogEntryForItemWithQrId(self, qrId, logEntryText, enteredByUserId,
                                   attachmentName, cherryPyData):
        item = self.itemDbApi.getItemByQrId(qrId)

        itemId = item.data['id']
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        # Add log entry
        itemElementLog = self.itemDbApi.addItemElementLog(
            selfElementId, logEntryText, enteredByUserId)
        logEntry = itemElementLog.data['log']

        # Check if log has an attachment that needs to be stored
        if attachmentName is not None and len(attachmentName) > 0:
            logId = logEntry.data['id']
            logAttachment = self.logControllerImpl.addLogAttachment(
                logId, attachmentName, None, enteredByUserId, cherryPyData)
            del (logAttachment.data['log'])
            logAttachmentJsonRep = logAttachment.getFullJsonRep()
            logEntry.data['logAttachmentAdded'] = logAttachmentJsonRep

        return logEntry

    def getLogEntriesForItemWithQrId(self, qrId):
        item = self.itemDbApi.getItemByQrId(qrId)
        itemId = item.data['id']
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        return self.logControllerImpl.getLogEntriesForItemElement(
            selfElementId)

    def getLogEntriesForItemWithId(self, itemId):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']
        return self.logControllerImpl.getLogEntriesForItemElement(
            selfElementId)

    def getCatalogItems(self):
        return self.itemDbApi.getItemsOfDomain(self.CATALOG_ITEM_DOMAIN_NAME)

    def getItemsDerivedFromItemId(self, derivedFromItemId):
        return self.itemDbApi.getItemsDerivedFromItem(derivedFromItemId)

    def getPropertiesForItemId(self, itemId):
        selfElement = self.itemDbApi.getSelfElementByItemId(itemId)
        selfElementId = selfElement.data['id']

        return self.propertyDbApi.getPropertyValueListForItemElementId(
            selfElementId)