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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi.impl;
24 import java.util.Properties;
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;
44 * This class acts as a facade for operations towards a policy model for task operations.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
55 // Apex model we're working towards
56 private final ApexModel apexModel;
58 // Properties to use for the model
59 private final Properties apexProperties;
61 // Facade classes for working towards the real Apex model
62 private final KeyInformationFacade keyInformationFacade;
64 // JSON output on list/delete if set
65 private final boolean jsonMode;
68 * Constructor that creates a task facade for the Apex Model API.
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
75 public TaskFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
76 this.apexModel = apexModel;
77 this.apexProperties = apexProperties;
78 this.jsonMode = jsonMode;
80 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
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
92 public ApexApiResult createTask(final String name, final String version, final String uuid,
93 final String description) {
95 final AxArtifactKey key = new AxArtifactKey();
97 if (version != null) {
98 key.setVersion(version);
100 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
103 if (apexModel.getPolicyModel().getTasks().getTaskMap().containsKey(key)) {
104 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
107 apexModel.getPolicyModel().getTasks().getTaskMap().put(key, new AxTask(key));
109 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
110 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
112 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
114 } catch (final Exception e) {
115 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
128 public ApexApiResult updateTask(final String name, final String version, final String uuid,
129 final String description) {
131 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
133 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
134 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
137 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
138 } catch (final Exception e) {
139 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
150 public ApexApiResult listTask(final String name, final String version) {
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);
158 final ApexApiResult result = new ApexApiResult();
159 for (final AxTask task : taskSet) {
160 result.addMessage(new ApexModelStringWriter<AxTask>(false).writeString(task, AxTask.class, jsonMode));
163 } catch (final Exception e) {
164 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
175 public ApexApiResult deleteTask(final String name, final String version) {
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));
184 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
185 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
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);
202 } catch (final Exception e) {
203 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
214 public ApexApiResult validateTask(final String name, final String version) {
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);
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());
230 } catch (final Exception e) {
231 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
236 * Create logic for a task.
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
242 * @param logic the source code for the logic of the task
243 * @return result of the operation
245 public ApexApiResult createTaskLogic(final String name, final String version, final String logicFlavour,
246 final String logic) {
248 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
250 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
251 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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");
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);
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);
270 * Update logic for a task.
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
278 public ApexApiResult updateTaskLogic(final String name, final String version, final String logicFlavour,
279 final String logic) {
281 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
283 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
284 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
292 final AxTaskLogic taskLogic = task.getTaskLogic();
293 if (logicFlavour != null) {
294 taskLogic.setLogicFlavour(logicFlavour);
297 taskLogic.setLogic(logic);
300 return new ApexApiResult();
301 } catch (final Exception e) {
302 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
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
313 public ApexApiResult listTaskLogic(final String name, final String version) {
315 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
317 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
318 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
329 * Delete logic for a task.
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
335 public ApexApiResult deleteTaskLogic(final String name, final String version) {
337 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
339 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
340 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
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());
353 } catch (final Exception e) {
354 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
359 * Create a task input field.
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
367 * @param optional true if the task field is optional, false otherwise
368 * @return result of the operation
370 public ApexApiResult createTaskInputField(final String name, final String version, final String fieldName,
371 final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
373 Assertions.argumentNotNull(fieldName, "fieldName may not be null");
375 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
377 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
378 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
381 final AxReferenceKey refKey =
382 new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), "inputFields", fieldName);
384 if (task.getInputFields().containsKey(refKey.getLocalName())) {
385 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
386 CONCEPT + refKey.getId() + ALREADY_EXISTS);
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);
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);
404 * List task input fields.
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
410 * @return result of the operation
412 public ApexApiResult listTaskInputField(final String name, final String version, final String fieldName) {
414 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
416 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
417 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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,
427 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
428 CONCEPT + name + ':' + version + ':' + inputField + DOES_NOT_EXIST);
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());
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));
443 } catch (final Exception e) {
444 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
450 * Delete a task input field.
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
457 public ApexApiResult deleteTaskInputField(final String name, final String version, final String fieldName) {
459 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
461 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
462 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
473 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
474 CONCEPT + name + ':' + version + ':' + fieldName + DOES_NOT_EXIST);
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());
482 for (final AxInputField field : task.getInputFields().values()) {
483 result.addMessage(new ApexModelStringWriter<AxInputField>(false).writeString(field,
484 AxInputField.class, jsonMode));
486 task.getInputFields().clear();
489 } catch (final Exception e) {
490 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
496 * Create a task output field.
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
504 * @param optional true if the task field is optional, false otherwise
505 * @return result of the operation
507 public ApexApiResult createTaskOutputField(final String name, final String version, final String fieldName,
508 final String contextSchemaName, final String contextSchemaVersion, final boolean optional) {
510 Assertions.argumentNotNull(fieldName, "fieldName may not be null");
512 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
514 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
515 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
518 final AxReferenceKey refKey =
519 new AxReferenceKey(task.getKey().getName(), task.getKey().getVersion(), "outputFields", fieldName);
521 if (task.getOutputFields().containsKey(refKey.getLocalName())) {
522 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
523 CONCEPT + refKey.getId() + ALREADY_EXISTS);
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);
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);
541 * List task output fields.
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
547 * @return result of the operation
549 public ApexApiResult listTaskOutputField(final String name, final String version, final String fieldName) {
551 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
553 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
554 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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));
564 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
565 CONCEPT + name + ':' + version + ':' + outputField + DOES_NOT_EXIST);
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());
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));
580 } catch (final Exception e) {
581 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
586 * Delete a task output field.
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
593 public ApexApiResult deleteTaskOutputField(final String name, final String version, final String fieldName) {
595 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
597 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
598 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
609 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
610 CONCEPT + name + ':' + version + ':' + fieldName + DOES_NOT_EXIST);
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());
618 for (final AxOutputField field : task.getOutputFields().values()) {
619 result.addMessage(new ApexModelStringWriter<AxOutputField>(false).writeString(field,
620 AxOutputField.class, jsonMode));
622 task.getOutputFields().clear();
625 } catch (final Exception e) {
626 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
631 * Create a task parameter.
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
639 public ApexApiResult createTaskParameter(final String name, final String version, final String parName,
640 final String defaultValue) {
642 Assertions.argumentNotNull(parName, "parName may not be null");
644 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
646 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
647 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
650 final AxReferenceKey refKey = new AxReferenceKey(task.getKey(), parName);
652 if (task.getTaskParameters().containsKey(refKey.getLocalName())) {
653 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
654 CONCEPT + refKey.getId() + ALREADY_EXISTS);
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);
665 * List task parameters.
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
672 public ApexApiResult listTaskParameter(final String name, final String version, final String parName) {
674 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
676 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
677 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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));
687 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
688 CONCEPT + name + ':' + version + ':' + taskParameter + DOES_NOT_EXIST);
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());
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));
703 } catch (final Exception e) {
704 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
709 * Delete a task parameter.
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
716 public ApexApiResult deleteTaskParameter(final String name, final String version, final String parName) {
718 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
720 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
721 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
732 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
733 CONCEPT + name + ':' + version + ':' + parName + DOES_NOT_EXIST);
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());
741 for (final AxTaskParameter parameter : task.getTaskParameters().values()) {
742 result.addMessage(new ApexModelStringWriter<AxTaskParameter>(false).writeString(parameter,
743 AxTaskParameter.class, jsonMode));
745 task.getTaskParameters().clear();
748 } catch (final Exception e) {
749 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
754 * Create a task context album reference.
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
763 public ApexApiResult createTaskContextRef(final String name, final String version, final String contextAlbumName,
764 final String contextAlbumVersion) {
766 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
768 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
769 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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);
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");
784 task.getContextAlbumReferences().add(contextAlbum.getKey());
785 return new ApexApiResult();
786 } catch (final Exception e) {
787 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
792 * List task context album references.
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
802 public ApexApiResult listTaskContextRef(final String name, final String version, final String contextAlbumName,
803 final String contextAlbumVersion) {
805 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
807 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
808 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
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))) {
818 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(albumKey,
819 AxArtifactKey.class, jsonMode));
823 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
824 CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
827 } catch (final Exception e) {
828 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
833 * Delete a task context album reference.
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
843 public ApexApiResult deleteTaskContextRef(final String name, final String version, final String contextAlbumName,
844 final String contextAlbumVersion) {
846 final AxTask task = apexModel.getPolicyModel().getTasks().get(name, version);
848 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
849 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
852 final Set<AxArtifactKey> deleteSet = new TreeSet<>();
854 for (final AxArtifactKey albumKey : task.getContextAlbumReferences()) {
855 if ((contextAlbumName != null && !albumKey.getName().equals(contextAlbumName))
856 || (contextAlbumVersion != null && !albumKey.getVersion().equals(contextAlbumVersion))) {
859 deleteSet.add(albumKey);
862 if (deleteSet.isEmpty()) {
863 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
864 CONCEPT + contextAlbumName + ':' + contextAlbumVersion + DOES_NOT_EXIST);
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));
873 } catch (final Exception e) {
874 return new ApexApiResult(ApexApiResult.Result.FAILED, e);