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53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
namespace ProcessMaker\BusinessModel;

class MessageEventRelation
{
    private $arrayFieldDefinition = array(
        "MSGER_UID"     => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "messageEventRelationUid"),

        "PRJ_UID"       => array("type" => "string", "required" => false, "empty" => false, "defaultValues" => array(), "fieldNameAux" => "projectUid"),
        "EVN_UID_THROW" => array("type" => "string", "required" => true,  "empty" => false, "defaultValues" => array(), "fieldNameAux" => "eventUidThrow"),
        "EVN_UID_CATCH" => array("type" => "string", "required" => true,  "empty" => false, "defaultValues" => array(), "fieldNameAux" => "eventUidCatch")
    );

    private $formatFieldNameInUppercase = true;

    private $arrayFieldNameForException = array();

    /**
     * Constructor of the class
     *
     * return void
     */
    public function __construct()
    {
        try {
            foreach ($this->arrayFieldDefinition as $key => $value) {
                $this->arrayFieldNameForException[$value["fieldNameAux"]] = $key;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Set the format of the fields name (uppercase, lowercase)
     *
     * @param bool $flag Value that set the format
     *
     * return void
     */
    public function setFormatFieldNameInUppercase($flag)
    {
        try {
            $this->formatFieldNameInUppercase = $flag;

            $this->setArrayFieldNameForException($this->arrayFieldNameForException);
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Set exception messages for fields
     *
     * @param array $arrayData Data with the fields
     *
     * return void
     */
    public function setArrayFieldNameForException(array $arrayData)
    {
        try {
            foreach ($arrayData as $key => $value) {
                $this->arrayFieldNameForException[$key] = $this->getFieldNameByFormatFieldName($value);
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Get the name of the field according to the format
     *
     * @param string $fieldName Field name
     *
     * return string Return the field name according the format
     */
    public function getFieldNameByFormatFieldName($fieldName)
    {
        try {
            return ($this->formatFieldNameInUppercase)? strtoupper($fieldName) : strtolower($fieldName);
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Verify if exists the Message-Event-Relation
     *
     * @param string $messageEventRelationUid Unique id of Message-Event-Relation
     *
     * return bool Return true if exists the Message-Event-Relation, false otherwise
     */
    public function exists($messageEventRelationUid)
    {
        try {
            $obj = \MessageEventRelationPeer::retrieveByPK($messageEventRelationUid);

            return (!is_null($obj))? true : false;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Verify if exists the Event-Relation of a Message-Event-Relation
     *
     * @param string $projectUid                       Unique id of Project
     * @param string $eventUidThrow                    Unique id of Event (throw)
     * @param string $eventUidCatch                    Unique id of Event (catch)
     * @param string $messageEventRelationUidToExclude Unique id of Message-Event-Relation to exclude
     *
     * return bool Return true if exists the Event-Relation of a Message-Event-Relation, false otherwise
     */
    public function existsEventRelation($projectUid, $eventUidThrow, $eventUidCatch, $messageEventRelationUidToExclude = "")
    {
        try {
            $criteria = new \Criteria("workflow");

            $criteria->addSelectColumn(\MessageEventRelationPeer::MSGER_UID);
            $criteria->add(\MessageEventRelationPeer::PRJ_UID, $projectUid, \Criteria::EQUAL);

            if ($messageEventRelationUidToExclude != "") {
                $criteria->add(\MessageEventRelationPeer::MSGER_UID, $messageEventRelationUidToExclude, \Criteria::NOT_EQUAL);
            }

            $criteria->add(\MessageEventRelationPeer::EVN_UID_THROW, $eventUidThrow, \Criteria::EQUAL);
            $criteria->add(\MessageEventRelationPeer::EVN_UID_CATCH, $eventUidCatch, \Criteria::EQUAL);

            $rsCriteria = \MessageEventRelationPeer::doSelectRS($criteria);

            return ($rsCriteria->next())? true : false;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Verify if does not exists the Message-Event-Relation
     *
     * @param string $messageEventRelationUid Unique id of Message-Event-Relation
     * @param string $fieldNameForException   Field name for the exception
     *
     * return void Throw exception if does not exists the Message-Event-Relation
     */
    public function throwExceptionIfNotExistsMessageEventRelation($messageEventRelationUid, $fieldNameForException)
    {
        try {
            if (!$this->exists($messageEventRelationUid)) {
                throw new \Exception(\G::LoadTranslation("ID_MESSAGE_EVENT_RELATION_DOES_NOT_EXIST", array($fieldNameForException, $messageEventRelationUid)));
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Verify if is registered the Event-Relation
     *
     * @param string $projectUid                       Unique id of Project
     * @param string $eventUidThrow                    Unique id of Event (throw)
     * @param string $eventUidCatch                    Unique id of Event (catch)
     * @param string $messageEventRelationUidToExclude Unique id of Message-Event-Relation to exclude
     *
     * return void Throw exception if is registered the Event-Relation
     */
    public function throwExceptionIfEventRelationIsRegistered($projectUid, $eventUidThrow, $eventUidCatch, $messageEventRelationUidToExclude = "")
    {
        try {
            if ($this->existsEventRelation($projectUid, $eventUidThrow, $eventUidCatch, $messageEventRelationUidToExclude)) {
                throw new \Exception(\G::LoadTranslation("ID_MESSAGE_EVENT_RELATION_ALREADY_REGISTERED"));
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Validate the data if they are invalid (INSERT and UPDATE)
     *
     * @param string $messageEventRelationUid Unique id of Message-Event-Relation
     * @param string $projectUid              Unique id of Project
     * @param array  $arrayData               Data
     *
     * return void Throw exception if data has an invalid value
     */
    public function throwExceptionIfDataIsInvalid($messageEventRelationUid, $projectUid, array $arrayData)
    {
        try {
            //Set variables
            $arrayMessageEventRelationData = ($messageEventRelationUid == "")? array() : $this->getMessageEventRelation($messageEventRelationUid, true);
            $flagInsert = ($messageEventRelationUid == "")? true : false;

            $arrayFinalData = array_merge($arrayMessageEventRelationData, $arrayData);

            //Verify data - Field definition
            $process = new \ProcessMaker\BusinessModel\Process();

            $process->throwExceptionIfDataNotMetFieldDefinition($arrayData, $this->arrayFieldDefinition, $this->arrayFieldNameForException, $flagInsert);

            //Verify data
            if (isset($arrayData["EVN_UID_THROW"]) || isset($arrayData["EVN_UID_CATCH"])) {
                $this->throwExceptionIfEventRelationIsRegistered($projectUid, $arrayFinalData["EVN_UID_THROW"], $arrayFinalData["EVN_UID_CATCH"], $messageEventRelationUid);
            }

            if (isset($arrayData["EVN_UID_THROW"]) || isset($arrayData["EVN_UID_CATCH"])) {
                //Flow
                $bpmnFlow = \BpmnFlow::findOneBy(array(
                    \BpmnFlowPeer::FLO_TYPE                => "MESSAGE",
                    \BpmnFlowPeer::FLO_ELEMENT_ORIGIN      => $arrayFinalData["EVN_UID_THROW"],
                    \BpmnFlowPeer::FLO_ELEMENT_ORIGIN_TYPE => "bpmnEvent",
                    \BpmnFlowPeer::FLO_ELEMENT_DEST        => $arrayFinalData["EVN_UID_CATCH"],
                    \BpmnFlowPeer::FLO_ELEMENT_DEST_TYPE   => "bpmnEvent"
                ));

                if (is_null($bpmnFlow)) {
                    throw new \Exception(\G::LoadTranslation(
                        "ID_MESSAGE_EVENT_RELATION_DOES_NOT_EXIST_MESSAGE_FLOW",
                        array(
                            $this->arrayFieldNameForException["eventUidThrow"], $arrayFinalData["EVN_UID_THROW"],
                            $this->arrayFieldNameForException["eventUidCatch"], $arrayFinalData["EVN_UID_CATCH"]
                        )
                    ));
                }

                //Check and validate Message Flow
                $bpmn = new \ProcessMaker\Project\Bpmn();

                $bpmn->throwExceptionFlowIfIsAnInvalidMessageFlow(array(
                    "FLO_TYPE"                => "MESSAGE",
                    "FLO_ELEMENT_ORIGIN"      => $arrayFinalData["EVN_UID_THROW"],
                    "FLO_ELEMENT_ORIGIN_TYPE" => "bpmnEvent",
                    "FLO_ELEMENT_DEST"        => $arrayFinalData["EVN_UID_CATCH"],
                    "FLO_ELEMENT_DEST_TYPE"   => "bpmnEvent"
                ));
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Create Message-Event-Relation for a Project
     *
     * @param string $projectUid Unique id of Project
     * @param array  $arrayData  Data
     *
     * return array Return data of the new Message-Event-Relation created
     */
    public function create($projectUid, array $arrayData)
    {
        try {
            //Verify data
            $process = new \ProcessMaker\BusinessModel\Process();
            $validator = new \ProcessMaker\BusinessModel\Validator();

            $validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
            $validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");

            //Set data
            $arrayData = array_change_key_case($arrayData, CASE_UPPER);

            unset($arrayData["MSGER_UID"]);
            unset($arrayData["PRJ_UID"]);

            //Verify data
            $process->throwExceptionIfNotExistsProcess($projectUid, $this->arrayFieldNameForException["projectUid"]);

            $this->throwExceptionIfDataIsInvalid("", $projectUid, $arrayData);

            //Create
            $cnn = \Propel::getConnection("workflow");

            try {
                $messageEventRelation = new \MessageEventRelation();

                $messageEventRelationUid = \ProcessMaker\Util\Common::generateUID();

                $messageEventRelation->fromArray($arrayData, \BasePeer::TYPE_FIELDNAME);

                $messageEventRelation->setMsgerUid($messageEventRelationUid);
                $messageEventRelation->setPrjUid($projectUid);

                if ($messageEventRelation->validate()) {
                    $cnn->begin();

                    $result = $messageEventRelation->save();

                    $cnn->commit();

                    //Return
                    return $this->getMessageEventRelation($messageEventRelationUid);
                } else {
                    $msg = "";

                    foreach ($messageEventRelation->getValidationFailures() as $validationFailure) {
                        $msg = $msg . (($msg != "")? "\n" : "") . $validationFailure->getMessage();
                    }

                    throw new \Exception(\G::LoadTranslation("ID_RECORD_CANNOT_BE_CREATED") . (($msg != "")? "\n" . $msg : ""));
                }
            } catch (\Exception $e) {
                $cnn->rollback();

                throw $e;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Delete Message-Event-Relation
     *
     * @param array $arrayCondition Conditions
     *
     * return void
     */
    public function deleteWhere(array $arrayCondition)
    {
        try {
            //Delete
            $criteria = new \Criteria("workflow");

            foreach ($arrayCondition as $key => $value) {
                if (is_array($value)) {
                    $criteria->add($key, $value[0], $value[1]);
                } else {
                    $criteria->add($key, $value, \Criteria::EQUAL);
                }
            }

            $result = \MessageEventRelationPeer::doDelete($criteria);
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Get criteria for Message-Event-Relation
     *
     * return object
     */
    public function getMessageEventRelationCriteria()
    {
        try {
            $criteria = new \Criteria("workflow");

            $criteria->addSelectColumn(\MessageEventRelationPeer::MSGER_UID);
            $criteria->addSelectColumn(\MessageEventRelationPeer::PRJ_UID);
            $criteria->addSelectColumn(\MessageEventRelationPeer::EVN_UID_THROW);
            $criteria->addSelectColumn(\MessageEventRelationPeer::EVN_UID_CATCH);

            return $criteria;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Get data of a Message-Event-Relation from a record
     *
     * @param array $record Record
     *
     * return array Return an array with data Message-Event-Relation
     */
    public function getMessageEventRelationDataFromRecord(array $record)
    {
        try {
            return array(
                $this->getFieldNameByFormatFieldName("MSGER_UID")     => $record["MSGER_UID"],
                $this->getFieldNameByFormatFieldName("EVN_UID_THROW") => $record["EVN_UID_THROW"],
                $this->getFieldNameByFormatFieldName("EVN_UID_CATCH") => $record["EVN_UID_CATCH"]
            );
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Get data of a Message-Event-Relation
     *
     * @param string $messageEventRelationUid Unique id of Message-Event-Relation
     * @param bool   $flagGetRecord           Value that set the getting
     *
     * return array Return an array with data of a Message-Event-Relation
     */
    public function getMessageEventRelation($messageEventRelationUid, $flagGetRecord = false)
    {
        try {
            //Verify data
            $this->throwExceptionIfNotExistsMessageEventRelation($messageEventRelationUid, $this->arrayFieldNameForException["messageEventRelationUid"]);

            //Get data
            $criteria = $this->getMessageEventRelationCriteria();

            $criteria->add(\MessageEventRelationPeer::MSGER_UID, $messageEventRelationUid, \Criteria::EQUAL);

            $rsCriteria = \MessageEventRelationPeer::doSelectRS($criteria);
            $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC);

            $rsCriteria->next();

            $row = $rsCriteria->getRow();

            //Return
            return (!$flagGetRecord)? $this->getMessageEventRelationDataFromRecord($row) : $row;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    /**
     * Get data of a Message-Event-Relation
     *
     * @param array $arrayCondition Conditions
     * @param bool  $flagGetRecord  Value that set the getting
     *
     * return array Return an array with data of a Message-Event-Relation, otherwise null
     */
    public function getMessageEventRelationWhere(array $arrayCondition, $flagGetRecord = false)
    {
        try {
            //Get data
            $criteria = $this->getMessageEventRelationCriteria();

            foreach ($arrayCondition as $key => $value) {
                if (is_array($value)) {
                    $criteria->add($key, $value[0], $value[1]);
                } else {
                    $criteria->add($key, $value, \Criteria::EQUAL);
                }
            }

            $rsCriteria = \MessageEventRelationPeer::doSelectRS($criteria);
            $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC);

            if ($rsCriteria->next()) {
                $row = $rsCriteria->getRow();

                //Return
                return (!$flagGetRecord)? $this->getMessageEventRelationDataFromRecord($row) : $row;
            } else {
                //Return
                return null;
            }
        } catch (\Exception $e) {
            throw $e;
        }
    }
}