Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / TaskFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.modelapi.impl;
23
24 import java.util.Properties;
25 import java.util.Set;
26 import java.util.TreeSet;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
31 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
34 import org.onap.policy.apex.model.eventmodel.concepts.AxInputField;
35 import org.onap.policy.apex.model.eventmodel.concepts.AxOutputField;
36 import org.onap.policy.apex.model.modelapi.ApexApiResult;
37 import org.onap.policy.apex.model.modelapi.ApexModel;
38 import org.onap.policy.apex.model.policymodel.concepts.AxTask;
39 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
40 import org.onap.policy.apex.model.policymodel.concepts.AxTaskParameter;
41 import org.onap.policy.common.utils.validation.Assertions;
42
43 /**
44  * This class acts as a facade for operations towards a policy model for task operations.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class TaskFacade {
49     private static final String CONCEPT = "concept ";
50     private static final String CONCEPT_S = "concept(s) ";
51     private static final String DOES_NOT_EXIST = " does not exist";
52     private static final String DO_ES_NOT_EXIST = " do(es) not exist";
53     private static final String ALREADY_EXISTS = " already exists";
54
55     // Apex model we're working towards
56     private final ApexModel apexModel;
57
58     // Properties to use for the model
59     private final Properties apexProperties;
60
61     // Facade classes for working towards the real Apex model
62     private final KeyInformationFacade keyInformationFacade;
63
64     // JSON output on list/delete if set
65     private final boolean jsonMode;
66
67     /**
68      * Constructor that creates a task facade for the Apex Model API.
69      *
70      * @param apexModel the apex model
71      * @param apexProperties Properties for the model
72      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
73      *        set to false
74      */
75     public TaskFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
76         this.apexModel = apexModel;
77         this.apexProperties = apexProperties;
78         this.jsonMode = jsonMode;
79
80         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
81     }
82
83     /**
84      * Create a task.
85      *
86      * @param name name of the task
87      * @param version version of the task, set to null to use the default version
88      * @param uuid task UUID, set to null to generate a UUID
89      * @param description task description, set to null to generate a description
90      * @return result of the operation
91      */
92     public ApexApiResult createTask(final String name, final String version, final String uuid,
93             final String description) {
94         try {
95             final AxArtifactKey key = new AxArtifactKey();
96             key.setName(name);
97             if (version != null) {
98                 key.setVersion(version);
99             } else {
100                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
101             }
102
103             if (apexModel.getPolicyModel().getTasks().getTaskMap().containsKey(key)) {
104                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
105             }
106
107             apexModel.getPolicyModel().getTasks().getTaskMap().put(key, new AxTask(key));
108
109             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
110                 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
111             } else {
112                 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
113             }
114         } catch (final Exception e) {
115             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
116         }
117     }
118
119     /**
120      * Update a task.
121      *
122      * @param name name of the task
123      * @param version version of the task, set to null to use the latest version
124      * @param uuid task UUID, set to null to not update
125      * @param description task description, set to null to not update
126      * @return result of the operation
127      */
128     public ApexApiResult updateTask(final String name, final String version, final String uuid,
129             final String description) {
130         try {
131             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
132             if (task == null) {
133                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
134                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
135             }
136
137             return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
138         } catch (final Exception e) {
139             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
140         }
141     }
142
143     /**
144      * List tasks.
145      *
146      * @param name name of the task, set to null to list all
147      * @param version starting version of the task, set to null to list all versions
148      * @return result of the operation
149      */
150     public ApexApiResult listTask(final String name, final String version) {
151         try {
152             final Set<AxTask> taskSet = apexModel.getPolicyModel().getTasks().getAll(name, version);
153             if (name != null && taskSet.isEmpty()) {
154                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
155                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
156             }
157
158             final ApexApiResult result = new ApexApiResult();
159             for (final AxTask task : taskSet) {
160                 result.addMessage(new ApexModelStringWriter<AxTask>(false).writeString(task, AxTask.class, jsonMode));
161             }
162             return result;
163         } catch (final Exception e) {
164             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
165         }
166     }
167
168     /**
169      * Delete a task.
170      *
171      * @param name name of the task
172      * @param version version of the task, set to null to use the latest version
173      * @return result of the operation
174      */
175     public ApexApiResult deleteTask(final String name, final String version) {
176         try {
177             if (version != null) {
178                 final AxArtifactKey key = new AxArtifactKey(name, version);
179                 final AxTask removedTask = apexModel.getPolicyModel().getTasks().getTaskMap().remove(key);
180                 if (removedTask != null) {
181                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
182                             new ApexModelStringWriter<AxTask>(false).writeString(removedTask, AxTask.class, jsonMode));
183                 } else {
184                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
185                             CONCEPT + key.getId() + DOES_NOT_EXIST);
186                 }
187             }
188
189             final Set<AxTask> taskSet = apexModel.getPolicyModel().getTasks().getAll(name, version);
190             if (taskSet.isEmpty()) {
191                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
192                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
193             }
194
195             final ApexApiResult result = new ApexApiResult();
196             for (final AxTask task : taskSet) {
197                 result.addMessage(new ApexModelStringWriter<AxTask>(false).writeString(task, AxTask.class, jsonMode));
198                 apexModel.getPolicyModel().getTasks().getTaskMap().remove(task.getKey());
199                 keyInformationFacade.deleteKeyInformation(name, version);
200             }
201             return result;
202         } catch (final Exception e) {
203             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
204         }
205     }
206
207     /**
208      * Validate tasks.
209      *
210      * @param name name of the task, set to null to list all
211      * @param version starting version of the task, set to null to list all versions
212      * @return result of the operation
213      */
214     public ApexApiResult validateTask(final String name, final String version) {
215         try {
216             final Set<AxTask> taskSet = apexModel.getPolicyModel().getTasks().getAll(name, version);
217             if (taskSet.isEmpty()) {
218                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
219                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
220             }
221
222             final ApexApiResult result = new ApexApiResult();
223             for (final AxTask task : taskSet) {
224                 final AxValidationResult validationResult = task.validate(new AxValidationResult());
225                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(task.getKey(),
226                         AxArtifactKey.class, jsonMode));
227                 result.addMessage(validationResult.toString());
228             }
229             return result;
230         } catch (final Exception e) {
231             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
232         }
233     }
234
235     /**
236      * Create logic for a task.
237      *
238      * @param name name of the task
239      * @param version version of the task, set to null to use the latest version
240      * @param logicFlavour the task logic flavour for the task, set to null to use the default task
241      *        logic flavour
242      * @param logic the source code for the logic of the task
243      * @return result of the operation
244      */
245     public ApexApiResult createTaskLogic(final String name, final String version, final String logicFlavour,
246             final String logic) {
247         try {
248             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
249             if (task == null) {
250                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
251                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
252             }
253
254             // There is only one logic item associated with a task so we use a hard coded logic name
255             final AxReferenceKey refKey = new AxReferenceKey(task.getKey(), "TaskLogic");
256
257             if (!task.getTaskLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
258                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
259                         CONCEPT + refKey.getId() + ALREADY_EXISTS);
260             }
261
262             task.setTaskLogic(new AxTaskLogic(refKey, logicFlavour, logic));
263             return new ApexApiResult();
264         } catch (final Exception e) {
265             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
266         }
267     }
268
269     /**
270      * Update logic for a task.
271      *
272      * @param name name of the task
273      * @param version version of the task, set to null to use the latest version
274      * @param logicFlavour the task logic flavour for the task, set to null to not update
275      * @param logic the source code for the logic of the task, set to null to not update
276      * @return result of the operation
277      */
278     public ApexApiResult updateTaskLogic(final String name, final String version, final String logicFlavour,
279             final String logic) {
280         try {
281             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
282             if (task == null) {
283                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
284                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
285             }
286
287             if (task.getTaskLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
288                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
289                         CONCEPT + task.getTaskLogic().getKey().getId() + DOES_NOT_EXIST);
290             }
291
292             final AxTaskLogic taskLogic = task.getTaskLogic();
293             if (logicFlavour != null) {
294                 taskLogic.setLogicFlavour(logicFlavour);
295             }
296             if (logic != null) {
297                 taskLogic.setLogic(logic);
298             }
299
300             return new ApexApiResult();
301         } catch (final Exception e) {
302             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
303         }
304     }
305
306     /**
307      * List task logic.
308      *
309      * @param name name of the task
310      * @param version version of the task, set to null to list the latest version
311      * @return result of the operation
312      */
313     public ApexApiResult listTaskLogic(final String name, final String version) {
314         try {
315             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
316             if (task == null) {
317                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
318                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
319             }
320
321             return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxTaskLogic>(false)
322                     .writeString(task.getTaskLogic(), AxTaskLogic.class, jsonMode));
323         } catch (final Exception e) {
324             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
325         }
326     }
327
328     /**
329      * Delete logic for a task.
330      *
331      * @param name name of the task
332      * @param version version of the task, set to null to use the latest version
333      * @return result of the operation
334      */
335     public ApexApiResult deleteTaskLogic(final String name, final String version) {
336         try {
337             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
338             if (task == null) {
339                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
340                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
341             }
342
343             if (task.getTaskLogic().getKey().getLocalName().equals(AxKey.NULL_KEY_NAME)) {
344                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
345                         CONCEPT + task.getTaskLogic().getKey().getId() + DOES_NOT_EXIST);
346             }
347
348             final ApexApiResult result = new ApexApiResult();
349             result.addMessage(new ApexModelStringWriter<AxTaskLogic>(false).writeString(task.getTaskLogic(),
350                     AxTaskLogic.class, jsonMode));
351             task.setTaskLogic(new AxTaskLogic());
352             return result;
353         } catch (final Exception e) {
354             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
355         }
356     }
357
358     /**
359      * Create a task input field.
360      *
361      * @param name name of the task
362      * @param version version of the task, set to null to use the latest version
363      * @param fieldName of the input field
364      * @param contextSchemaName name of the input field context schema
365      * @param contextSchemaVersion version of the input field context schema, set to null to use the
366      *        latest version
367      * @param optional true if the task field is optional, false otherwise
368      * @return result of the operation
369      */
370     public ApexApiResult createTaskInputField(final String name, final String version, final String fieldName,
371             final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
372         try {
373             Assertions.argumentNotNull(fieldName, "fieldName may not be null");
374
375             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
376             if (task == null) {
377                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
378                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
379             }
380
381             final AxReferenceKey refKey =
382                     new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), "inputFields", fieldName);
383
384             if (task.getInputFields().containsKey(refKey.getLocalName())) {
385                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
386                         CONCEPT + refKey.getId() + ALREADY_EXISTS);
387             }
388
389             final AxContextSchema schema =
390                     apexModel.getPolicyModel().getSchemas().get(contextSchemaName, contextSchemaVersion);
391             if (schema == null) {
392                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
393                         CONCEPT + contextSchemaName + ':' + contextSchemaVersion + DOES_NOT_EXIST);
394             }
395
396             task.getInputFields().put(refKey.getLocalName(), new AxInputField(refKey, schema.getKey(), optional));
397             return new ApexApiResult();
398         } catch (final Exception e) {
399             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
400         }
401     }
402
403     /**
404      * List task input fields.
405      *
406      * @param name name of the task
407      * @param version version of the task, set to null to use the latest version
408      * @param fieldName field name of the input field, set to null to list all input fields of the
409      *        task
410      * @return result of the operation
411      */
412     public ApexApiResult listTaskInputField(final String name, final String version, final String fieldName) {
413         try {
414             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
415             if (task == null) {
416                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
417                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
418             }
419
420             if (fieldName != null) {
421                 final AxInputField inputField = task.getInputFields().get(fieldName);
422                 if (inputField != null) {
423                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
424                             new ApexModelStringWriter<AxInputField>(false).writeString(inputField, AxInputField.class,
425                                     jsonMode));
426                 } else {
427                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
428                             CONCEPT + name + ':' + version + ':' + inputField + DOES_NOT_EXIST);
429                 }
430             } else {
431                 if (task.getInputFields().size() == 0) {
432                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
433                             "no input fields defined on task " + task.getKey().getId());
434                 }
435
436                 final ApexApiResult result = new ApexApiResult();
437                 for (final AxInputField field : task.getInputFields().values()) {
438                     result.addMessage(new ApexModelStringWriter<AxInputField>(false).writeString(field,
439                             AxInputField.class, jsonMode));
440                 }
441                 return result;
442             }
443         } catch (final Exception e) {
444             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
445         }
446
447     }
448
449     /**
450      * Delete a task input field.
451      *
452      * @param name name of the task
453      * @param version version of the task, set to null to use the latest version
454      * @param fieldName of the input field, set to null to delete all input fields
455      * @return result of the operation
456      */
457     public ApexApiResult deleteTaskInputField(final String name, final String version, final String fieldName) {
458         try {
459             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
460             if (task == null) {
461                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
462                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
463             }
464
465             final ApexApiResult result = new ApexApiResult();
466             if (fieldName != null) {
467                 if (task.getInputFields().containsKey(fieldName)) {
468                     result.addMessage(new ApexModelStringWriter<AxInputField>(false)
469                             .writeString(task.getInputFields().get(fieldName), AxInputField.class, jsonMode));
470                     task.getInputFields().remove(fieldName);
471                     return result;
472                 } else {
473                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
474                             CONCEPT + name + ':' + version + ':' + fieldName + DOES_NOT_EXIST);
475                 }
476             } else {
477                 if (task.getInputFields().size() == 0) {
478                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
479                             "no input fields defined on task " + task.getKey().getId());
480                 }
481
482                 for (final AxInputField field : task.getInputFields().values()) {
483                     result.addMessage(new ApexModelStringWriter<AxInputField>(false).writeString(field,
484                             AxInputField.class, jsonMode));
485                 }
486                 task.getInputFields().clear();
487                 return result;
488             }
489         } catch (final Exception e) {
490             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
491         }
492
493     }
494
495     /**
496      * Create a task output field.
497      *
498      * @param name name of the task
499      * @param version version of the task, set to null to use the latest version
500      * @param fieldName of the output field
501      * @param contextSchemaName name of the output field context schema
502      * @param contextSchemaVersion version of the output field context schema, set to null to use
503      *        the latest version
504      * @param optional true if the task field is optional, false otherwise
505      * @return result of the operation
506      */
507     public ApexApiResult createTaskOutputField(final String name, final String version, final String fieldName,
508             final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
509         try {
510             Assertions.argumentNotNull(fieldName, "fieldName may not be null");
511
512             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
513             if (task == null) {
514                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
515                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
516             }
517
518             final AxReferenceKey refKey =
519                     new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), "outputFields", fieldName);
520
521             if (task.getOutputFields().containsKey(refKey.getLocalName())) {
522                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
523                         CONCEPT + refKey.getId() + ALREADY_EXISTS);
524             }
525
526             final AxContextSchema schema =
527                     apexModel.getPolicyModel().getSchemas().get(contextSchemaName, contextSchemaVersion);
528             if (schema == null) {
529                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
530                         CONCEPT + contextSchemaName + ':' + contextSchemaVersion + DOES_NOT_EXIST);
531             }
532
533             task.getOutputFields().put(refKey.getLocalName(), new AxOutputField(refKey, schema.getKey(), optional));
534             return new ApexApiResult();
535         } catch (final Exception e) {
536             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
537         }
538     }
539
540     /**
541      * List task output fields.
542      *
543      * @param name name of the task
544      * @param version version of the task, set to null to use the latest version
545      * @param fieldName field name of the output field, set to null to list all output fields of the
546      *        task
547      * @return result of the operation
548      */
549     public ApexApiResult listTaskOutputField(final String name, final String version, final String fieldName) {
550         try {
551             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
552             if (task == null) {
553                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
554                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
555             }
556
557             if (fieldName != null) {
558                 final AxOutputField outputField = task.getOutputFields().get(fieldName);
559                 if (outputField != null) {
560                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
561                             new ApexModelStringWriter<AxOutputField>(false).writeString(outputField,
562                                     AxOutputField.class, jsonMode));
563                 } else {
564                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
565                             CONCEPT + name + ':' + version + ':' + outputField + DOES_NOT_EXIST);
566                 }
567             } else {
568                 if (task.getOutputFields().size() == 0) {
569                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
570                             "no output fields defined on task " + task.getKey().getId());
571                 }
572
573                 final ApexApiResult result = new ApexApiResult();
574                 for (final AxOutputField field : task.getOutputFields().values()) {
575                     result.addMessage(new ApexModelStringWriter<AxOutputField>(false).writeString(field,
576                             AxOutputField.class, jsonMode));
577                 }
578                 return result;
579             }
580         } catch (final Exception e) {
581             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
582         }
583     }
584
585     /**
586      * Delete a task output field.
587      *
588      * @param name name of the task
589      * @param version version of the task, set to null to use the latest version
590      * @param fieldName of the output field, set to null to delete all output fields
591      * @return result of the operation
592      */
593     public ApexApiResult deleteTaskOutputField(final String name, final String version, final String fieldName) {
594         try {
595             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
596             if (task == null) {
597                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
598                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
599             }
600
601             final ApexApiResult result = new ApexApiResult();
602             if (fieldName != null) {
603                 if (task.getOutputFields().containsKey(fieldName)) {
604                     result.addMessage(new ApexModelStringWriter<AxOutputField>(false)
605                             .writeString(task.getOutputFields().get(fieldName), AxOutputField.class, jsonMode));
606                     task.getOutputFields().remove(fieldName);
607                     return result;
608                 } else {
609                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
610                             CONCEPT + name + ':' + version + ':' + fieldName + DOES_NOT_EXIST);
611                 }
612             } else {
613                 if (task.getOutputFields().size() == 0) {
614                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
615                             "no output fields defined on task " + task.getKey().getId());
616                 }
617
618                 for (final AxOutputField field : task.getOutputFields().values()) {
619                     result.addMessage(new ApexModelStringWriter<AxOutputField>(false).writeString(field,
620                             AxOutputField.class, jsonMode));
621                 }
622                 task.getOutputFields().clear();
623                 return result;
624             }
625         } catch (final Exception e) {
626             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
627         }
628     }
629
630     /**
631      * Create a task parameter.
632      *
633      * @param name name of the task
634      * @param version version of the task, set to null to use the latest version
635      * @param parName of the parameter
636      * @param defaultValue of the parameter
637      * @return result of the operation
638      */
639     public ApexApiResult createTaskParameter(final String name, final String version, final String parName,
640             final String defaultValue) {
641         try {
642             Assertions.argumentNotNull(parName, "parName may not be null");
643
644             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
645             if (task == null) {
646                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
647                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
648             }
649
650             final AxReferenceKey refKey = new AxReferenceKey(task.getKey(), parName);
651
652             if (task.getTaskParameters().containsKey(refKey.getLocalName())) {
653                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
654                         CONCEPT + refKey.getId() + ALREADY_EXISTS);
655             }
656
657             task.getTaskParameters().put(refKey.getLocalName(), new AxTaskParameter(refKey, defaultValue));
658             return new ApexApiResult();
659         } catch (final Exception e) {
660             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
661         }
662     }
663
664     /**
665      * List task parameters.
666      *
667      * @param name name of the task
668      * @param version version of the task, set to null to use the latest version
669      * @param parName name of the parameter, set to null to list all parameters of the task
670      * @return result of the operation
671      */
672     public ApexApiResult listTaskParameter(final String name, final String version, final String parName) {
673         try {
674             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
675             if (task == null) {
676                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
677                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
678             }
679
680             if (parName != null) {
681                 final AxTaskParameter taskParameter = task.getTaskParameters().get(parName);
682                 if (taskParameter != null) {
683                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
684                             new ApexModelStringWriter<AxTaskParameter>(false).writeString(taskParameter,
685                                     AxTaskParameter.class, jsonMode));
686                 } else {
687                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
688                             CONCEPT + name + ':' + version + ':' + taskParameter + DOES_NOT_EXIST);
689                 }
690             } else {
691                 if (task.getTaskParameters().size() == 0) {
692                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
693                             "no task parameters defined on task " + task.getKey().getId());
694                 }
695
696                 final ApexApiResult result = new ApexApiResult();
697                 for (final AxTaskParameter parameter : task.getTaskParameters().values()) {
698                     result.addMessage(new ApexModelStringWriter<AxTaskParameter>(false).writeString(parameter,
699                             AxTaskParameter.class, jsonMode));
700                 }
701                 return result;
702             }
703         } catch (final Exception e) {
704             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
705         }
706     }
707
708     /**
709      * Delete a task parameter.
710      *
711      * @param name name of the task
712      * @param version version of the task, set to null to use the latest version
713      * @param parName of the parameter, set to null to delete all task parameters
714      * @return result of the operation
715      */
716     public ApexApiResult deleteTaskParameter(final String name, final String version, final String parName) {
717         try {
718             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
719             if (task == null) {
720                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
721                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
722             }
723
724             final ApexApiResult result = new ApexApiResult();
725             if (parName != null) {
726                 if (task.getTaskParameters().containsKey(parName)) {
727                     result.addMessage(new ApexModelStringWriter<AxTaskParameter>(false)
728                             .writeString(task.getTaskParameters().get(parName), AxTaskParameter.class, jsonMode));
729                     task.getTaskParameters().remove(parName);
730                     return result;
731                 } else {
732                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
733                             CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
734                 }
735             } else {
736                 if (task.getTaskParameters().size() == 0) {
737                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
738                             "no task parameters defined on task " + task.getKey().getId());
739                 }
740
741                 for (final AxTaskParameter parameter : task.getTaskParameters().values()) {
742                     result.addMessage(new ApexModelStringWriter<AxTaskParameter>(false).writeString(parameter,
743                             AxTaskParameter.class, jsonMode));
744                 }
745                 task.getTaskParameters().clear();
746                 return result;
747             }
748         } catch (final Exception e) {
749             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
750         }
751     }
752
753     /**
754      * Create a task context album reference.
755      *
756      * @param name name of the task
757      * @param version version of the task, set to null to use the latest version
758      * @param contextAlbumName name of the context album for the context album reference
759      * @param contextAlbumVersion version of the context album for the context album reference, set
760      *        to null to use the latest version
761      * @return result of the operation
762      */
763     public ApexApiResult createTaskContextRef(final String name, final String version, final String contextAlbumName,
764             final String contextAlbumVersion) {
765         try {
766             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
767             if (task == null) {
768                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
769                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
770             }
771
772             final AxContextAlbum contextAlbum =
773                     apexModel.getPolicyModel().getAlbums().get(contextAlbumName, contextAlbumVersion);
774             if (contextAlbum == null) {
775                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
776                         CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
777             }
778
779             if (task.getContextAlbumReferences().contains(contextAlbum.getKey())) {
780                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, "context album reference for concept "
781                         + contextAlbum.getKey().getId() + " already exists in task");
782             }
783
784             task.getContextAlbumReferences().add(contextAlbum.getKey());
785             return new ApexApiResult();
786         } catch (final Exception e) {
787             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
788         }
789     }
790
791     /**
792      * List task context album references.
793      *
794      * @param name name of the task
795      * @param version version of the task, set to null to use the latest version
796      * @param contextAlbumName name of the context album for the context album reference, set to
797      *        null to list all task context album references
798      * @param contextAlbumVersion version of the context album for the context album reference, set
799      *        to null to use the latest version
800      * @return result of the operation
801      */
802     public ApexApiResult listTaskContextRef(final String name, final String version, final String contextAlbumName,
803             final String contextAlbumVersion) {
804         try {
805             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
806             if (task == null) {
807                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
808                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
809             }
810
811             final ApexApiResult result = new ApexApiResult();
812             boolean found = false;
813             for (final AxArtifactKey albumKey : task.getContextAlbumReferences()) {
814                 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
815                         || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
816                     continue;
817                 }
818                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(albumKey,
819                         AxArtifactKey.class, jsonMode));
820                 found = true;
821             }
822             if (!found) {
823                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
824                         CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
825             }
826             return result;
827         } catch (final Exception e) {
828             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
829         }
830     }
831
832     /**
833      * Delete a task context album reference.
834      *
835      * @param name name of the task
836      * @param version version of the task, set to null to use the latest version
837      * @param contextAlbumName name of the context album for the context album reference, set to
838      *        null to delete all task context album references
839      * @param contextAlbumVersion version of the context album for the context album reference, set
840      *        to null to use the latest version
841      * @return result of the operation
842      */
843     public ApexApiResult deleteTaskContextRef(final String name, final String version, final String contextAlbumName,
844             final String contextAlbumVersion) {
845         try {
846             final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
847             if (task == null) {
848                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
849                         CONCEPT + name + ':' + version + DOES_NOT_EXIST);
850             }
851
852             final Set<AxArtifactKey> deleteSet = new TreeSet<>();
853
854             for (final AxArtifactKey albumKey : task.getContextAlbumReferences()) {
855                 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
856                         || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
857                     continue;
858                 }
859                 deleteSet.add(albumKey);
860             }
861
862             if (deleteSet.isEmpty()) {
863                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
864                         CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
865             }
866             final ApexApiResult result = new ApexApiResult();
867             for (final AxArtifactKey keyToDelete : deleteSet) {
868                 task.getContextAlbumReferences().remove(keyToDelete);
869                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyToDelete,
870                         AxArtifactKey.class, jsonMode));
871             }
872             return result;
873         } catch (final Exception e) {
874             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
875         }
876     }
877 }