public function createError(\Exception $ex)
 {
     if ($ex instanceof \Application\Exceptions\ValidateException) {
         $error = $this->_getFirstError($ex->getValidationErrors(), $field, $message);
         // Error message has a valid UNICA format?
         if ($message && !preg_match('/^Supported values are (.+)/', $message)) {
             $message = '';
         }
     } else {
         $field = '#unknownField#';
         $error = '#unknownError#';
     }
     $errorModel = new ErrorModel(array('fault' => array('code' => FaultModel::FAULT_CODE_CLIENT, 'description' => "Generic Validation fault"), 'code' => 1021, 'category' => ErrorModel::ERROR_CATEGORY_SVC, 'description' => $field ? 'Invalid parameter value: ' . $field . ". " . $message : $ex->getMessage()));
     switch ($error) {
         case \Zend_Validate_StringLength::TOO_SHORT:
         case \Zend_Validate_StringLength::TOO_LONG:
             $errorModel->description = "Invalid '" . $field . "' length." . $message;
             $errorModel->code = 1011;
             break;
         case \Zend_Validate_InArray::NOT_IN_ARRAY:
             switch ($field) {
                 case 'lifeCycleState':
                     $errorModel->description = "Invalid parameter value: {$field}. Supported values are " . implode(",", LifeCycleModel::getStatuses());
                     break;
                 case 'simType':
                     $errorModel->description = "Invalid parameter value: {$field}. Supported values are " . implode(",", array(\Application\Model\SimModel::SIM_TYPE_GLOBAL, \Application\Model\SimModel::SIM_TYPE_LOCAL));
                     break;
                 default:
                     $errorModel->description = "Invalid parameter value: {$field}";
                     break;
             }
             break;
         case 'badPeriod':
             $errorModel->description = "Invalid parameter value: {$field}.";
             break;
         case \App_Validate_NotEmptyIfField::IS_EMPTY:
             $errorModel->description = "Needed parameter was not found: {$field}.";
             $errorModel->code = 1020;
             break;
     }
     return $errorModel;
 }
 public static function getValidatorSpec()
 {
     return array(self::LIFE_CYCLE_STATE => array('InArray' => array('haystack' => \Application\Model\LifeCycleModel::getStatuses(), 'strict' => true, 'breakChainOnFailure' => true)), self::REGION_ID => array('Int' => array('breakChainOnFailure' => true), 'Enum_Region' => array('breakChainOnFailure' => true)), self::COMMERCIAL_GROUP => array('Int' => array('breakChainOnFailure' => true)), self::BILLING_ACCOUNT => array('Int' => array('breakChainOnFailure' => true)), self::SIM_TYPE => array('NotEmpty' => array('breakChainOnFailure' => true), 'SimType' => array('breakChainOnFailure' => true)), self::OPERATOR => array('StringLength' => array('max' => 6, 'min' => 1, 'encoding' => "UTF-8", 'breakChainOnFailure' => true)), self::STATIC_IP => array('Ip' => array('breakChainOnFailure' => true)), self::EXPENSE_VOICE => self::getExpenseSpec(), self::EXPENSE_DATA => self::getExpenseSpec(), self::EXPENSE_SMS => self::getExpenseSpec(), self::EXPENSE_TOTAL => self::getExpenseSpec(), self::CUSTOMER => array('Ericsson_OrganizationExistsById' => array('organizationType' => \Application\Model\Organization\OrgCustomerModel::ORG_TYPE, 'breakChainOnFailure' => true)), self::LTE_ENABLED => array('NotEmpty' => array('breakChainOnFailure' => true), 'InArray' => array('haystack' => array('true', 'false', '1', '0'), 'strict' => true, 'breakChainOnFailure' => true)));
 }
 /**
  * @param $state
  * @param $data array|App_FilterList
  * @return void
  */
 public function changeLifeCycleStateSync($oldState, $newState, $simId)
 {
     if (!in_array($oldState, LifeCycleModel::getStatuses()) || !in_array($newState, LifeCycleModel::getStatuses())) {
         throw new InvalidArgumentException("Invalid state");
     }
     $mapper = SimMapper::getInstance();
     if ($newState === LifeCycleModel::STATUS_RESTORE) {
         $result = $mapper->restoreSuspendedLifeCycleStateSync($simId);
     } else {
         //find operation cost
         $cost = $mapper->getLifeCycleStateCost($oldState, $newState, $simId);
         $result = $mapper->changeLifeCycleStateSync($oldState, $newState, $simId, $cost);
     }
     \App::audit('Changed life cycle status on sims to ' . $newState . '. Handler: ' . $result, null);
     return $result;
 }
 /**
  * Update a sim identified by its Id
  */
 public function putAction()
 {
     $sim = $this->_getSim();
     $attr = $this->getRequest()->getParam('attribute');
     $value = $this->getRequest()->getParam('value');
     if (is_null($value)) {
         throw new InvalidArgumentException("Invalid value.");
     }
     $this->_helper->allowed('update', $sim);
     switch ($attr) {
         case 'alias':
             $sim->setAlias($value);
             break;
         case 'billingAccount':
             throw new DeprecatedException('Parameter "billingAccount" is deprecated.');
             break;
         case 'customField1':
             $sim->setCustomField_1($value);
             break;
         case 'customField2':
             $sim->setCustomField_2($value);
             break;
         case 'customField3':
             $sim->setCustomField_3($value);
             break;
         case 'customField4':
             $sim->setCustomField_4($value);
             break;
         case 'commercialGroup':
             $commercialGroup = CommercialGroupService::getInstance()->load($value);
             $this->_helper->allowed('read', $commercialGroup);
             $this->_helper->allowed('assign_sim', $commercialGroup);
             $this->_simSrv->changeCommercialGroup($commercialGroup, array($sim->getId()));
             $this->getResponse()->setHttpResponseCode(204);
             return;
         case 'lifeCycleStatus':
             // Generic permission
             $this->_helper->allowed('change_lifecycle_status', $sim);
             // Validate status
             if (!in_array($value, LifeCycleModel::getStatuses())) {
                 throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
             }
             // Status permission
             switch ($value) {
                 case LifeCycleModel::STATUS_RETIRED:
                 case LifeCycleModel::STATUS_SUSPENDED:
                     $this->_helper->allowed('change_lifecycle_status_to_suspended_or_retired', $sim);
                     break;
                 case LifeCycleModel::STATUS_DEACTIVATED:
                     $this->_helper->allowed('change_lifecycle_status_to_deactivated', $sim);
                     break;
                 case LifeCycleModel::STATUS_RESTORE:
                     $this->_helper->allowed('restore_suspended_lifecycle_status', $sim);
                     break;
             }
             // Change status
             $this->_simSrv->changeLifeCycleStateSync($sim->lifeCycleStatus, $value, $sim->getId());
             $this->getResponse()->setHttpResponseCode(204);
             return;
         default:
             throw new InvalidArgumentException("Invalid attribute");
     }
     $this->_simSrv->update($sim);
     $this->getResponse()->setHttpResponseCode(204);
 }
 public function createError(\Exception $ex)
 {
     if ($ex instanceof ValidateException) {
         $error = $this->_getFirstError($ex->getValidationErrors(), $field, $message);
         // Error message has a valid UNICA format?
         $origMessage = $message;
         if ($message && !preg_match('/^Supported values are (.+)/', $message)) {
             $message = '';
         }
     } else {
         $field = '#unknownField#';
         $error = '#unknownError#';
     }
     $exMessage = preg_replace('/(.*)filter\\s*\'(\\w+)\': (.*)/', '$1parameter value: $2.', $ex->getMessage());
     $errorModel = new ErrorModel(array('fault' => array('code' => FaultModel::FAULT_CODE_CLIENT, 'description' => "Generic Validation fault"), 'code' => 1021, 'category' => ErrorModel::ERROR_CATEGORY_SVC, 'description' => $field ? 'Invalid parameter value: ' . $field . ". " . $message : $exMessage));
     switch ($error) {
         case 'customerId':
             //TODO this validation should throw a NotFoundException instead
             //of a ValidationException this is a very dirty approach to the
             //problem
             if (preg_match("/No\\sorganization\\s'(?P<customerId>[[:ascii:]]+)'\\swas\\sfound/", $origMessage, $matches)) {
                 $message = sprintf("Resource %s does not exist", $matches['customerId']);
                 $errorModel->description = $message;
                 $errorModel->code = 1006;
             }
             break;
             //[Apism2m] SVC 1020 o SVC 1021
             //According mail from apism2m list, change 1020 code to 1021.
             //            case \Zend_Validate_NotEmpty::IS_EMPTY:
             //                $errorModel->description = "Needed parameter not found: '" . $field . "' - " . $message;
             //                $errorModel->code = 1020;
             //                break;
         //[Apism2m] SVC 1020 o SVC 1021
         //According mail from apism2m list, change 1020 code to 1021.
         //            case \Zend_Validate_NotEmpty::IS_EMPTY:
         //                $errorModel->description = "Needed parameter not found: '" . $field . "' - " . $message;
         //                $errorModel->code = 1020;
         //                break;
         case \Zend_Validate_StringLength::TOO_LONG:
             if (preg_match('/is more than (?P<num>\\d+) characters long/', $origMessage, $matches)) {
                 $message = 'Length should be less than ' . $matches['num'] . ' characters';
             }
             $errorModel->description = "Invalid '" . $field . "' length. " . $message;
             $errorModel->code = 1011;
             break;
         case \Zend_Validate_InArray::NOT_IN_ARRAY:
             switch ($field) {
                 case 'lifeCycleState':
                     $errorModel->description = "Invalid parameter value: {$field}. Supported values are " . implode(",", LifeCycleModel::getStatuses());
                     break;
                 case 'simType':
                     $errorModel->description = "Invalid parameter value: {$field}. Supported values are " . implode(",", array(\Application\Model\SimModel::SIM_TYPE_GLOBAL, \Application\Model\SimModel::SIM_TYPE_LOCAL));
                     break;
                 default:
                     $errorModel->description = "Invalid parameter value: {$field}";
                     break;
             }
             break;
         case 'badPeriod':
             $errorModel->description = "Invalid parameter value: {$field}.";
             break;
         case \App_Validate_Enum_Currency::CURRENCY:
             $errorModel->description = "Invalid parameter value: {$field}.";
             break;
         case \App_Validate_NotEmptyIfField::IS_EMPTY:
             $errorModel->description = "Needed parameter was not found: {$field}.";
             $errorModel->code = 1020;
             break;
         case \App_Validate_GSM0338Charset::INVALID_CHAR:
             $errorModel->code = 1012;
             break;
     }
     return $errorModel;
 }
 /**
  * Update a sim identified by its Id
  */
 public function putAction()
 {
     $sim = $this->_getSim();
     $data = $this->_helper->requestData();
     if (is_object($data)) {
         $data = get_object_vars($data);
     }
     $this->_helper->allowed('update', $sim);
     $apnsInfoModified = false;
     foreach ($data as $attr => $value) {
         if (is_null($value)) {
             throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
         }
         switch ($attr) {
             case 'alias':
                 $this->_helper->allowedField('update_field', 'alias', $sim);
                 $sim->setAlias($value);
                 break;
             case 'customField1':
                 $this->_helper->allowedField('update_field', 'customField_1', $sim);
                 $sim->setCustomField_1($value);
                 break;
             case 'customField2':
                 $this->_helper->allowedField('update_field', 'customField_2', $sim);
                 $sim->setCustomField_2($value);
                 break;
             case 'customField3':
                 $this->_helper->allowedField('update_field', 'customField_4', $sim);
                 $sim->setCustomField_3($value);
                 break;
             case 'customField4':
                 $this->_helper->allowedField('update_field', 'customField_4', $sim);
                 $sim->setCustomField_4($value);
                 break;
             case 'staticIp':
                 $this->_helper->allowedField('update_field', 'staticIpAddress', $sim);
                 $sim->setStaticIpAddress($value["ipv4"]);
                 $apnsInfoModified = true;
                 break;
             case 'staticApnIndex':
                 $this->_helper->allowedField('update_field', 'apns', $sim);
                 if ($value != -1) {
                     $value++;
                 }
                 $sim->setStaticIpApnIndex($value);
                 $apnsInfoModified = true;
                 break;
             case 'apn0':
             case 'apn1':
             case 'apn2':
             case 'apn3':
             case 'apn4':
             case 'apn5':
             case 'apn6':
             case 'apn7':
             case 'apn8':
             case 'apn9':
                 $this->_helper->allowedField('update_field', 'apns', $sim);
                 //pre validate apn
                 $apnValidator = new App_Validate_SimApn();
                 if ($value == '-') {
                     $value = "";
                 }
                 $isValid = $apnValidator->isValid($value, $sim->exportData());
                 if (!$isValid) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}");
                 }
                 $apnId = substr($attr, 3, 1);
                 $apns = $sim->getApns();
                 if ($apns[$apnId] != $value) {
                     $apns[$apnId] = $value;
                     $sim->setApns($apns);
                     $apnsInfoModified = true;
                 }
                 break;
             case 'commercialGroup':
                 if (empty($value)) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}. Commercial group must be a not empty string.");
                 }
                 $commercialGroup = CommercialGroupService::getInstance()->load($value);
                 $this->_helper->allowed('read', $commercialGroup);
                 $this->_helper->allowed('assign_sim', $commercialGroup);
                 if ($sim->customerId != $commercialGroup->customerId) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}. Sim CustomerId distinct to commercial Group customerId");
                 }
                 // Change commercial group
                 $this->_simSrv->changeCommercialGroup($commercialGroup, array($sim->getId()), true);
                 // End request
                 $this->getResponse()->setHttpResponseCode(204);
                 $this->_helper->viewRenderer->setNoRender(TRUE);
                 return;
             case 'lifeCycleStatus':
                 // Generic permission
                 $this->_helper->allowed('change_lifecycle_status', $sim);
                 // Validate status
                 if (!in_array($value, LifeCycleModel::getStatuses())) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
                 }
                 // Status permission
                 switch ($value) {
                     case LifeCycleModel::STATUS_RETIRED:
                     case LifeCycleModel::STATUS_SUSPENDED:
                         $this->_helper->allowed('change_lifecycle_status_to_suspended_or_retired', $sim);
                         break;
                     case LifeCycleModel::STATUS_DEACTIVATED:
                         $this->_helper->allowed('change_lifecycle_status_to_deactivated', $sim);
                         break;
                     case LifeCycleModel::STATUS_RESTORE:
                         $this->_helper->allowed('restore_suspended_lifecycle_status', $sim);
                         break;
                 }
                 // Change status
                 $this->_simSrv->changeLifeCycleStateSync($sim->lifeCycleStatus, $value, $sim->getId());
                 // End request
                 $this->getResponse()->setHttpResponseCode(204);
                 $this->_helper->viewRenderer->setNoRender(TRUE);
                 return;
             case 'dailyConsumptionThreshold':
             case 'monthlyConsumptionThreshold':
                 // Check permission
                 $this->_helper->allowed('update_consumption_control', $sim);
                 // Modification
                 $limits = new \Application\Model\Sim\ConsumptionControlLimitModel($this->_getLimits($value, $attr === 'dailyConsumptionThreshold' ? 'Daily' : 'Monthly'));
                 $this->_simSrv->updateConsumptionControl($limits, $sim);
                 break;
             case 'monthlyExpenseLimit':
                 // Check permission
                 $this->_helper->allowed('async_sim_change_expenselimit', $sim);
                 // Modification
                 $value = $this->_mapLimitDecimal($sim, $value);
                 $limits = new \Application\Model\Sim\ExpenseControlLimitModel($this->_getLimits($value));
                 $this->_simSrv->changeExpenseLimit($limits, $sim);
                 break;
             case 'lteEnabled':
                 // Check permission
                 $this->_helper->allowed('changeLte', $sim);
                 //Modification
                 $this->_simSrv->changeLteSync($value, $sim->getId());
                 break;
             default:
                 throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
         }
     }
     // When some data related with the apns are received we need to send all the related data
     // otherwise staticIpAddress is fixed to null to avoid to call to changeSimApn function and
     // staticIpApnIndex and Apns are fixed to null to avoid error validations.
     if (!$apnsInfoModified) {
         $sim->setStaticIpAddress(null);
         $sim->setStaticIpApnIndex(null);
         $sim->setApns(null);
     } else {
         $this->_validateAPNsInfo($sim, $data);
     }
     try {
         $this->_simSrv->update($sim);
     } catch (ValidateException $ex) {
         throw $this->_mapException($ex);
     }
     $this->getResponse()->setHttpResponseCode(204);
     $this->_helper->viewRenderer->setNoRender(TRUE);
 }
 public static function getValidatorSpec()
 {
     return array(self::LEGACY => array('NotEmpty' => array('breakChainOnFailure' => true), 'InArray' => array('haystack' => array('true', 'false', '1', '0'), 'strict' => true, 'breakChainOnFailure' => true)), self::LIFE_CYCLE_STATUS => array('InArray' => array('haystack' => \Application\Model\LifeCycleModel::getStatuses(), 'strict' => true, 'breakChainOnFailure' => true)), self::COMMERCIAL_GROUP => array('Int' => array('breakChainOnFailure' => true)));
 }