def save(self): """ Save case modified variables values """ if self.state == None: raise Exception("The Bonita case is not started") url = "/runtimeAPI/setProcessInstanceVariable/%s" % self.uuid for (key, value) in self.variables.items(): data = {} data["variableId"] = key data["variableValue"] = value BonitaServer.get_instance().sendRESTRequest(url=url, data=data)
def start(self, user=None): """ Start the case :param user: case actor used to start the case :type user: str """ if self.state != None: raise Exception("case already started on uuid %s" % self.uuid) data = dict() if self._variables == None: url = "/runtimeAPI/instantiateProcess/%s" % self._process.uuid else: url = "/runtimeAPI/instantiateProcessWithVariables/%s" % self._process.uuid data['variables'] = dictToMapString(self._variables) xml = BonitaServer.get_instance().sendRESTRequest(url=url, user=user, data=data) dom = parseString(xml) process_instances = dom.getElementsByTagName("ProcessInstanceUUID") if len(process_instances) != 1: raise Exception #FIXME: raise clear Exception values = process_instances[0].getElementsByTagName("value") if len(values) != 1: raise Exception #FIXME: raise clear Exception uuid = values[0].childNodes[0].data self.uuid = uuid self.refresh()
def add_attachment(self, name, descriptor=None, filename=None, filepath=None, description=None, user=None): """ Add an attachment to the current case instance :param name: Name of the attachment variable in the process :type name: str :param descriptor: File object to attach to the case (mandatory if no filepath is given) :type name: file :param filename: Name of the file to attach to the case (mandatory if no filepath is given) :type filename: str :param filepath: Path to the file to attach to the case (mandatory if no descriptor or filename is given) :type filepath: str :param user: Login of the actor to use to attach the file within the case (mandatory) :type user: str """ if self.state == None: raise Exception("The Bonita case is not started") #FIXME: check params types if descriptor == None and filepath == None: raise ValueError("add_attachment requires at least a descriptor or a filepath") if filename == None and filepath == None: raise ValueError("add_attachment requires at least a filename or a filepath") if filepath != None: filepath = os.path.expandvars(filepath) filepath = os.path.expanduser(filepath) if not os.path.exists(filepath) or not os.path.isfile(filepath): raise ValueError("invalid filepath : %s" % filepath) descriptor = open(filepath, "rb") if filename == None: filename = os.path.basename(filepath) import array bts = array.array('b', descriptor.read()) data = dict() data['value'] = bts data['fileName'] = filename url = "/runtimeAPI/addAttachment/%s/%s" % (self.uuid, name) BonitaServer.get_instance().sendRESTRequest(url=url, data=data, user=user)
def delete(self, user=None): """ Delete a BonitaObject : remove it from the Bonita server """ (url,data) = self._generate_delete_url() # Call the BonitaServer xml = BonitaServer.get_instance().sendRESTRequest(url = url, user=user, data=data)
def get(cls, uuid): """ Retrieve a case from its uuid :param uuid: uuid of the case to retrieve :type uuid: str :returns: BonitaCase -- the retrieved case """ url = "/queryRuntimeAPI/getProcessInstance/%s" % uuid xml = BonitaServer.get_instance().sendRESTRequest(url=url) return BonitaCase._instanciate_from_xml(xml)
def get(cls, uuid): """ Retrieve a process from its uuid :param uuid: uuid of the process to retrieve :type uuid: str :returns: BonitaProcess -- the retrieved process. """ url = "/queryDefinitionAPI/getProcess/%s" % uuid try: xml = BonitaServer.get_instance().sendRESTRequest(url=url) except BonitaHTTPError: return None return BonitaProcess._instanciate_from_xml(xml)
def get_cases(self): """ Get all existing cases from the process :returns: BonitaCase list """ url = "/queryRuntimeAPI/getProcessInstances/%s" % self.uuid xml = BonitaServer.get_instance().sendRESTRequest(url=url) soup = BeautifulSoup(xml.encode('iso-8859-1'),'xml') cases = [] for instance in soup.set.findAll('processinstance'): cases.append(BonitaCase._instanciate_from_xml(unicode(instance))) return cases
def save(self, user=None, variables=None): """ Save a BonitaObject : sends data to create a resource on the Bonita server. """ # Delegate generation of URL to subclasses (url,data) = self._generate_save_url(variables) # Call the BonitaServer xml = BonitaServer.get_instance().sendRESTRequest(url=url, user=user, data=data) # Extract UUID of newly created object soup = BeautifulSoup(xml,'xml') instances = soup.findAll("uuid") if len(instances) != 1: raise Exception #fixme: raise clear Exception self.uuid = instances[0].text
def get_processes(cls, process_id): """ Get all processes version for a given process id :param process_id: process id :type process_id: str :returns: BonitaProcess list """ url = "/queryDefinitionAPI/getProcessesByProcessId/%s" % process_id xml = BonitaServer.get_instance().sendRESTRequest(url=url) soup = BeautifulSoup(xml.encode('iso-8859-1'),'xml') processes = [] for definition in soup.set.findAll('ProcessDefinition'): processes.append(BonitaProcess._instanciate_from_xml(unicode(definition))) return processes
def refresh(self, xml=None): """ Refresh current instance with data from the BonitaServer """ if xml == None: url = "/queryRuntimeAPI/getProcessInstance/%s" % self.uuid xml = BonitaServer.get_instance().sendRESTRequest(url=url) soup = BeautifulSoup(xml.encode('iso-8859-1'),'xml') variables = {} for variable in soup.processinstance.clientvariables.findAll('entry'): strings = variable.findAll('string') if len(strings) == 2: variables[strings[0].text] = strings[1].text else: variables[strings[0].text] = None self._variables = variables self._state = soup.processinstance.state.text self._is_archived = False if soup.processinstance.isarchived.text == "false" else True self._started_date = datetime.fromtimestamp(float(soup.processinstance.starteddate.text) / 1000.0) self._last_update = datetime.fromtimestamp(float(soup.processinstance.lastupdate.text) / 1000.0)