import logging
from orangescape.model.engine.execution.ProcessEngine import ProcessNode
from orangescape.model.engine.exception.Error import CommandError
from orangescape.model.engine.exception.ErrorMessage import Action1001
from orangescape.model.engine.execution.ProcessUtil import getRootBranch
import appconfig as config

class NewProcessNode(ProcessNode):

    def enter(self):
        metadataId = self.args.get('Model-Name')
        if metadataId:
            metadata = self.getSheetMetadataById(metadataId)
        else:
            rootAction = getRootBranch(self.activityDefinition)
            raise CommandError, Action1001%(self.sheet.getSheetMetadata().getSheetName(), rootAction.getSheetName())
        params = {}
        for opPort in self.activityDefinition.getOutputportMetadataList():
            if self.args.has_key(opPort.getParamName()):
                value = self.args[opPort.getParamName()]
                cell = opPort.getCellMetadata()
                if cell and not cell.isFormula():
                    params[cell.getCellName()] = value
            else:
                """Value might not be specified or it might be None"""

        if config.env != config.SL and logging.getLogger().level == logging.DEBUG:
            logging.debug('Creating new instance for the model %s with parameters %s'%(metadata.getSheetName(), params))
        newSheet = self.component.new(metadata, params)

        self.update({'Reference-Cell':newSheet.getSheetId()})
