From 469bf2f6fb4c7a0bf04f38ae43380e174712141d Mon Sep 17 00:00:00 2001 From: Brayan Osmar Pereyra Suxo Date: Wed, 5 Mar 2014 16:28:21 -0400 Subject: [PATCH] Adicion de DEPARTAMENTS --- .../engine/src/BusinessModel/Departament.php | 173 ++++++++++++++++++ .../engine/src/BusinessModel/Validator.php | 125 +++++++++++++ .../Services/Api/ProcessMaker/Departament.php | 130 +++++++++++++ 3 files changed, 428 insertions(+) create mode 100644 workflow/engine/src/BusinessModel/Departament.php create mode 100644 workflow/engine/src/BusinessModel/Validator.php create mode 100644 workflow/engine/src/Services/Api/ProcessMaker/Departament.php diff --git a/workflow/engine/src/BusinessModel/Departament.php b/workflow/engine/src/BusinessModel/Departament.php new file mode 100644 index 000000000..535112a19 --- /dev/null +++ b/workflow/engine/src/BusinessModel/Departament.php @@ -0,0 +1,173 @@ + + * @copyright Colosa - Bolivia + */ +class Departament +{ + /** + * Get list for Departaments + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + public function getDepartaments() + { + $oDepartament = new \Department(); + $aDepts = $oDepartament->getDepartments(''); + foreach ($aDepts as &$depData) { + $depData['DEP_CHILDREN'] = $this->getChildren($depData); + $depData = array_change_key_case($depData, CASE_LOWER); + } + return $aDepts; + } + + /** + * Get list for Departaments + * @var string $dep_uid. Uid for Departament + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + public function getDepartament($dep_uid) + { + $dep_uid = Validator::depUid($dep_uid); + $criteria = new \Criteria( 'workflow' ); + $criteria->add( DepartmentPeer::DEP_UID, $dep_uid, \Criteria::EQUAL ); + $con = \Propel::getConnection( DepartmentPeer::DATABASE_NAME ); + $objects = DepartmentPeer::doSelect( $criteria, $con ); + + global $RBAC; + $node = array (); + foreach ($objects as $oDepartment) { + $node['DEP_UID'] = $oDepartment->getDepUid(); + $node['DEP_PARENT'] = $oDepartment->getDepParent(); + $node['DEP_TITLE'] = $oDepartment->getDepTitle(); + $node['DEP_STATUS'] = $oDepartment->getDepStatus(); + $node['DEP_MANAGER'] = $oDepartment->getDepManager(); + $node['DEP_LDAP_DN'] = $oDepartment->getDepLdapDn(); + $node['DEP_LAST'] = 0; + + $manager = $oDepartment->getDepManager(); + if ($manager != '') { + $UserUID = $RBAC->load( $manager ); + $node['DEP_MANAGER_USERNAME'] = isset( $UserUID['USR_USERNAME'] ) ? $UserUID['USR_USERNAME'] : ''; + $node['DEP_MANAGER_FIRSTNAME'] = isset( $UserUID['USR_FIRSTNAME'] ) ? $UserUID['USR_FIRSTNAME'] : ''; + $node['DEP_MANAGER_LASTNAME'] = isset( $UserUID['USR_LASTNAME'] ) ? $UserUID['USR_LASTNAME'] : ''; + } else { + $node['DEP_MANAGER_USERNAME'] = ''; + $node['DEP_MANAGER_FIRSTNAME'] = ''; + $node['DEP_MANAGER_LASTNAME'] = ''; + } + + $criteriaCount = new \Criteria( 'workflow' ); + $criteriaCount->clearSelectColumns(); + $criteriaCount->addSelectColumn( 'COUNT(*)' ); + $criteriaCount->add( DepartmentPeer::DEP_PARENT, $oDepartment->getDepUid(), \Criteria::EQUAL ); + $rs = DepartmentPeer::doSelectRS( $criteriaCount ); + $rs->next(); + $row = $rs->getRow(); + $node['HAS_CHILDREN'] = $row[0]; + } + $node = array_change_key_case($node, CASE_LOWER); + return $node; + } + + /** + * Save Departament + * @var string $dep_data. Data for Process + * @var string $create. Flag for create or update + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + public function saveDepartament($dep_data, $create = true) + { + $dep_data = array_change_key_case($dep_data, CASE_UPPER); + + $oDepartament = new \Department(); + if (isset($dep_data['DEP_UID']) && $dep_data['DEP_UID'] != '') { + Validator::depUid($dep_data['DEP_UID']); + } + if (isset($dep_data['DEP_TITLE'])) { + Validator::depTitle($dep_data['DEP_TITLE']); + } + if (isset($dep_data['DEP_PARENT']) && $dep_data['DEP_PARENT'] != '') { + Validator::depUid($dep_data['DEP_PARENT'], 'dep_parent'); + } + if (isset($dep_data['DEP_MANAGER']) && $dep_data['DEP_PARENT'] != '') { + Validator::usrUid($dep_data['DEP_MANAGER'], 'dep_manager'); + } + if (isset($dep_data['DEP_STATUS'])) { + Validator::depStatus($dep_data['DEP_STATUS']); + } + + if (!$create) { + $dep_data['DEPO_TITLE'] = $dep_data['DEP_TITLE']; + $oDepartament->update($dep_data); + $oDepartament->updateDepartmentManager($dep_data['DEP_UID']); + } else { + $dep_uid = $oDepartament->create($dep_data); + $response = $this->getDepartament($dep_uid); + return $response; + } + } + + /** + * Delete departament + * @var string $dep_uid. Uid for department + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + public function deleteDepartament($dep_uid) + { + $dep_uid = Validator::depUid($dep_uid); + $oDepartament = new \Department(); + $oDepartament->remove($dep_uid); + } + + /** + * Look for Children for department + * @var array $dataDep. Data for child department + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + protected function getChildren ($dataDep) + { + $children = array(); + if ((int)$dataDep['HAS_CHILDREN'] > 0) { + $oDepartament = new \Department(); + $aDepts = $oDepartament->getDepartments($dataDep['DEP_UID']); + foreach ($aDepts as &$depData) { + $depData['DEP_CHILDREN'] = $this->getChildren($depData); + $depData = array_change_key_case($depData, CASE_LOWER); + $children[] = $depData; + } + } + return $children; + } +} + diff --git a/workflow/engine/src/BusinessModel/Validator.php b/workflow/engine/src/BusinessModel/Validator.php new file mode 100644 index 000000000..b2cef466c --- /dev/null +++ b/workflow/engine/src/BusinessModel/Validator.php @@ -0,0 +1,125 @@ + + * @copyright Colosa - Bolivia + * + * @protected + */ +class Validator +{ + /** + * Validate dep_uid + * @var string $dep_uid. Uid for Departament + * @var string $nameField. Name of field for message + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return string + */ + static public function depUid($dep_uid, $nameField = 'dep_uid') + { + $dep_uid = trim($dep_uid); + if ($dep_uid == '') { + throw (new \Exception("The departament with $nameField: '' does not exist.")); + } + $oDepartment = new \Department(); + if (!($oDepartment->existsDepartment($dep_uid))) { + throw (new \Exception("The departament with $nameField: '$dep_uid' does not exist.")); + } + return $dep_uid; + } + + /** + * Validate dep_title + * @var string $dep_title. Name or Title for Departament + * @var string $dep_uid. Uid for Departament + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url GET + */ + static public function depTitle($dep_title, $dep_uid = '') + { + $dep_title = trim($dep_title); + if ($dep_title == '') { + throw (new \Exception("The departament with dep_title: '' is incorrect.")); + } + + $oCriteria = new \Criteria( 'workflow' ); + $oCriteria->clearSelectColumns(); + $oCriteria->addSelectColumn( \ContentPeer::CON_CATEGORY ); + $oCriteria->addSelectColumn( \ContentPeer::CON_VALUE ); + $oCriteria->addSelectColumn( \DepartmentPeer::DEP_PARENT ); + $oCriteria->add( \ContentPeer::CON_CATEGORY, 'DEPO_TITLE' ); + $oCriteria->addJoin( \ContentPeer::CON_ID, \DepartmentPeer::DEP_UID, \Criteria::LEFT_JOIN ); + $oCriteria->add( \ContentPeer::CON_VALUE, $dep_title ); + $oCriteria->add( \ContentPeer::CON_LANG, SYS_LANG ); + if ($dep_uid != '') { + $oCriteria->add( \ContentPeer::CON_ID, $dep_uid, \Criteria::NOT_EQUAL ); + } + + $oDataset = \DepartmentPeer::doSelectRS( $oCriteria ); + $oDataset->setFetchmode( \ResultSet::FETCHMODE_ASSOC ); + if ($oDataset->next()) { + throw (new \Exception("The departament with dep_title: '$dep_title' exist.")); + } + return $dep_title; + } + + /** + * Validate dep_status + * @var string $dep_uid. Uid for Departament + * @var string $nameField. Name of field for message + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return string + */ + static public function depStatus($dep_status) + { + $dep_status = trim($dep_status); + $values = array('ACTIVE', 'INACTIVE'); + if (!in_array($dep_status, $values)) { + throw (new \Exception("The departament with dep_status: '$dep_status' is incorrect.")); + } + return $dep_status; + } + + /** + * Validate usr_uid + * @var string $dep_uid. Uid for Departament + * @var string $nameField. Name of field for message + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return string + */ + static public function usrUid($usr_uid, $nameField = 'usr_uid') + { + $usr_uid = trim($usr_uid); + if ($usr_uid == '') { + throw (new \Exception("The user with $nameField: '' does not exist.")); + } + $oUsers = new \Users(); + if (!($oUsers->userExists($usr_uid))) { + throw (new \Exception("The user with $nameField: '$usr_uid' does not exist.")); + } + return $usr_uid; + } +} + + diff --git a/workflow/engine/src/Services/Api/ProcessMaker/Departament.php b/workflow/engine/src/Services/Api/ProcessMaker/Departament.php new file mode 100644 index 000000000..3fb36e2df --- /dev/null +++ b/workflow/engine/src/Services/Api/ProcessMaker/Departament.php @@ -0,0 +1,130 @@ + + * @copyright Colosa - Bolivia + * + * @protected + */ +class Departament extends Api +{ + /** + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url GET + */ + public function doGetDepartaments() + { + try { + $oDepartament = new \BusinessModel\Departament(); + $response = $oDepartament->getDepartaments(); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + + /** + * @param string $dep_uid {@min 1}{@max 32} + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url GET /:dep_uid + */ + public function doGetDepartament($dep_uid) + { + try { + $oDepartament = new \BusinessModel\Departament(); + $response = $oDepartament->getDepartament($dep_uid); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + + /** + * @param array $request_data + * @param string $dep_title {@from body} {@min 1} + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url POST + * @status 201 + */ + public function doPost($request_data, $dep_title) + { + try { + $oDepartament = new \BusinessModel\Departament(); + $response = $oDepartament->saveDepartament($request_data); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + + /** + * @param string $dep_uid {@min 1}{@max 32} + * + * @param array $request_data + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url PUT /:dep_uid + */ + public function doPut($dep_uid, $request_data) + { + try { + $request_data['dep_uid'] = $dep_uid; + $oDepartament = new \BusinessModel\Departament(); + $response = $oDepartament->saveDepartament($request_data, false); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + + /** + * @param string $dep_uid {@min 1}{@max 32} + * + * @access public + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + * + * @url DELETE /:dep_uid + */ + public function doDelete($dep_uid) + { + try { + $oDepartament = new \BusinessModel\Departament(); + $oDepartament->deleteDepartament($dep_uid); + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } +} +