re base code
[sdc.git] / openecomp-be / backend / openecomp-sdc-action-manager / src / main / java / org / openecomp / sdc / action / impl / ActionManagerImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.action.impl;
18
19 import org.apache.commons.collections4.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.openecomp.core.dao.UniqueValueDaoFactory;
22 import org.openecomp.core.util.UniqueValueUtil;
23 import org.openecomp.core.utilities.CommonMethods;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.action.ActionConstants;
26 import org.openecomp.sdc.action.ActionManager;
27 import org.openecomp.sdc.action.dao.ActionArtifactDao;
28 import org.openecomp.sdc.action.dao.ActionArtifactDaoFactory;
29 import org.openecomp.sdc.action.dao.ActionDao;
30 import org.openecomp.sdc.action.dao.ActionDaoFactory;
31 import org.openecomp.sdc.action.dao.types.ActionArtifactEntity;
32 import org.openecomp.sdc.action.dao.types.ActionEntity;
33 import org.openecomp.sdc.action.errors.ActionErrorConstants;
34 import org.openecomp.sdc.action.errors.ActionException;
35 import org.openecomp.sdc.action.logging.StatusCode;
36 import org.openecomp.sdc.action.types.*;
37 import org.openecomp.sdc.common.errors.CoreException;
38 import org.openecomp.sdc.logging.api.Logger;
39 import org.openecomp.sdc.logging.api.LoggerFactory;
40 import org.openecomp.sdc.versioning.ActionVersioningManager;
41 import org.openecomp.sdc.versioning.ActionVersioningManagerFactory;
42 import org.openecomp.sdc.versioning.dao.VersionInfoDao;
43 import org.openecomp.sdc.versioning.dao.VersionInfoDaoFactory;
44 import org.openecomp.sdc.versioning.dao.types.UserCandidateVersion;
45 import org.openecomp.sdc.versioning.dao.types.Version;
46 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
47 import org.openecomp.sdc.versioning.errors.EntityNotExistErrorBuilder;
48 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
49 import org.openecomp.sdc.versioning.types.VersionInfo;
50 import org.openecomp.sdc.versioning.types.VersionableEntityAction;
51 import org.slf4j.MDC;
52
53 import java.util.*;
54
55 import static org.openecomp.sdc.action.ActionConstants.*;
56 import static org.openecomp.sdc.action.errors.ActionErrorConstants.*;
57 import static org.openecomp.sdc.action.util.ActionUtil.*;
58 import static org.openecomp.sdc.versioning.dao.types.Version.VERSION_STRING_VIOLATION_MSG;
59
60 /**
61  * Manager Implementation for {@link ActionManager Action Library Operations} <br> Handles Business
62  * layer validations and acts as an interface between the REST and DAO layers.
63  */
64 public class ActionManagerImpl implements ActionManager {
65
66   private static final ActionDao actionDao = ActionDaoFactory.getInstance().createInterface();
67   private static final ActionVersioningManager versioningManager =
68       ActionVersioningManagerFactory.getInstance().createInterface();
69   private static final ActionArtifactDao actionArtifactDao =
70       ActionArtifactDaoFactory.getInstance().createInterface();
71   private static VersionInfoDao versionInfoDao =
72       VersionInfoDaoFactory.getInstance().createInterface();
73
74   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
75
76   public ActionManagerImpl() {
77     actionDao.registerVersioning(ACTION_VERSIONABLE_TYPE);
78   }
79
80   /**
81    * List All Major, Last Minor and Candidate version (if any) for Given Action Invariant UUID
82    *
83    * @param invariantId Invariant UUID of the action for which the information is required
84    * @return List of All Major, Last Minor and Candidate version if any Of {@link Action} with given
85    * actionInvariantUuId.
86    * @throws ActionException Exception with an action library specific code, short description and
87    *                         detailed message for the error occurred during the operation
88    */
89
90   @Override
91   public List<Action> getActionsByActionInvariantUuId(String invariantId) throws ActionException {
92     List<Action> actions;
93
94     log.debug(" entering getActionsByActionInvariantUuId with  invariantID = " + invariantId);
95     actions = actionDao
96         .getActionsByActionInvariantUuId(invariantId != null ? invariantId.toUpperCase() : null);
97
98     if (actions != null && actions.isEmpty()) {
99       throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
100     }
101
102     log.debug(" exit getActionsByActionInvariantUuId with  invariantID = " + invariantId);
103     return actions;
104   }
105
106   /**
107    * Get list of actions based on a filter criteria. If no filter is sent all actions will be
108    * returned
109    *
110    * @param filterType  Filter by Vendor/Category/Model/Component/None
111    * @param filterValue Filter Parameter Value (Vendor ID/Category ID/Model ID/Component ID)
112    * @return List of {@link Action} objects based on a filter criteria <br> Empty List if no records
113    * match the provided filter criteria
114    * @throws ActionException Exception with an action library specific code, short description and
115    *                         detailed message for the error occurred for the error occurred during
116    *                         the operation
117    */
118   @Override
119   public List<Action> getFilteredActions(String filterType, String filterValue)
120       throws ActionException {
121     List<Action> actions;
122     log.debug(" entering getFilteredActions By filterType = " + filterType + " With value = "
123         + filterValue);
124     switch (filterType) {
125       case FILTER_TYPE_NONE:
126         //Business validation for OPENECOMP Component type fetch (if any)
127         break;
128       case FILTER_TYPE_VENDOR:
129         //Business validation for vendor type fetch (if any)
130         break;
131       case FILTER_TYPE_CATEGORY:
132         //Business validation for Category type fetch (if any)
133         break;
134       case FILTER_TYPE_MODEL:
135         //Business validation for model type fetch (if any)
136         break;
137       case FILTER_TYPE_OPEN_ECOMP_COMPONENT:
138         //Business validation for OPENECOMP Component type fetch (if any)
139         break;
140       case FILTER_TYPE_NAME:
141         actions = actionDao
142             .getFilteredActions(filterType, filterValue != null ? filterValue.toLowerCase() : null);
143         if (actions != null && actions.isEmpty()) {
144           throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
145         }
146         log.debug(" exit getFilteredActions By filterType = " + filterType + " With value = "
147             + filterValue);
148         return actions;
149       default:
150         break;
151     }
152     actions = actionDao
153         .getFilteredActions(filterType, filterValue != null ? filterValue.toLowerCase() : null);
154     List<Action> majorMinorVersionList = getMajorMinorVersionActions(actions);
155     Collections.sort(majorMinorVersionList);
156     log.debug(
157         " exit getFilteredActions By filterType = " + filterType + " With value = " + filterValue);
158     return majorMinorVersionList;
159   }
160
161   /**
162    * Get the properties of an action version by its UUID.
163    *
164    * @param actionUuId UUID of the specific action version
165    * @return {@link Action} object corresponding the version represented by the UUID
166    * @throws ActionException Exception with an action library specific code, short description and
167    *                         detailed message for the error occurred for the error occurred during
168    *                         the operation
169    */
170   @Override
171   public Action getActionsByActionUuId(String actionUuId) throws ActionException {
172     log.debug(" entering getActionsByActionUuId with  actionUUID = " + actionUuId);
173     Action action =
174         actionDao.getActionsByActionUuId(actionUuId != null ? actionUuId.toUpperCase() : null);
175
176     if (action == null) {
177       throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
178     }
179
180     log.debug(" exit getActionsByActionUuId with  actionUUID = " + actionUuId);
181     return action;
182   }
183
184   /**
185    * List OPENECOMP Components supported by Action Library.
186    *
187    * @return List of {@link OpenEcompComponent} objects supported by Action Library <br> Empty List if
188    * no components are found
189    * @throws ActionException Exception with an action library specific code, short description and
190    *                         detailed message for the error occurred for the error occurred during
191    *                         the operation
192    */
193   @Override
194   public List<OpenEcompComponent> getOpenEcompComponents() throws ActionException {
195     return actionDao.getOpenEcompComponents();
196   }
197
198
199   /**
200    * Delete an action.
201    *
202    * @param actionInvariantUuId Invariant UUID of the action to be deleted
203    * @param user                User id of the user performing the operation
204    */
205   @Override
206   public void deleteAction(String actionInvariantUuId, String user) throws ActionException {
207     try {
208       log.debug("entering deleteAction with actionInvariantUuId = " + actionInvariantUuId
209           + " and user = " + user);
210       actionLogPreProcessor(ActionSubOperation.DELETE_ACTION, TARGET_ENTITY_API);
211       versioningManager.delete(ACTION_VERSIONABLE_TYPE, actionInvariantUuId, user);
212       actionLogPostProcessor(StatusCode.COMPLETE);
213       log.metrics("");
214       actionDao.deleteAction(actionInvariantUuId);
215     } catch (CoreException ce) {
216       formAndThrowException(ce);
217     }
218   }
219
220   /**
221    * Create a new Action.
222    *
223    * @param action Action object model of the user request for creating an action
224    * @param user   AT&T id of the user sending the create request
225    * @return {@link Action} model object for the created action
226    * @throws ActionException Exception with an action library specific code, short description and
227    *                         detailed message for the error occurred for the error occurred during
228    *                         the operation
229    */
230   @Override
231   public Action createAction(Action action, String user) throws ActionException {
232     UniqueValueUtil uniqueValueUtil =
233         new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface());
234     try {
235       actionLogPreProcessor(ActionSubOperation.VALIDATE_ACTION_UNIQUE_NAME, TARGET_ENTITY_API);
236       uniqueValueUtil
237           .validateUniqueValue(ActionConstants.UniqueValues.ACTION_NAME, action.getName());
238       actionLogPostProcessor(StatusCode.COMPLETE);
239     } catch (CoreException exception) {
240       String errorDesc = String
241           .format(ACTION_ENTITY_UNIQUE_VALUE_MSG, ActionConstants.UniqueValues.ACTION_NAME,
242               action.getName());
243       log.error(errorDesc, exception);
244       actionLogPostProcessor(StatusCode.ERROR, ACTION_ENTITY_UNIQUE_VALUE_ERROR, errorDesc, false);
245       throw new ActionException(ACTION_ENTITY_UNIQUE_VALUE_ERROR, errorDesc);
246     } finally {
247       log.metrics("");
248     }
249     action.setUser(user);
250     action.setTimestamp(getCurrentTimeStampUtc());
251     action.setActionInvariantUuId(CommonMethods.nextUuId());
252     action.setActionUuId(CommonMethods.nextUuId());
253
254     actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_VERSION, TARGET_ENTITY_API);
255     Version version =
256         versioningManager.create(ACTION_VERSIONABLE_TYPE, action.getActionInvariantUuId(), user);
257     actionLogPostProcessor(StatusCode.COMPLETE);
258     log.metrics("");
259
260     action.setVersion(version.toString());
261     action.setStatus(ActionStatus.Locked);
262     action = updateData(action);
263     action = actionDao.createAction(action);
264
265     actionLogPreProcessor(ActionSubOperation.CREATE_ACTION_UNIQUE_VALUE, TARGET_ENTITY_API);
266     uniqueValueUtil.createUniqueValue(ActionConstants.UniqueValues.ACTION_NAME, action.getName());
267     actionLogPostProcessor(StatusCode.COMPLETE);
268     log.metrics("");
269
270     return action;
271   }
272
273   /**
274    * Update an existing action.
275    *
276    * @param action Action object model of the user request for creating an action
277    * @param user   AT&T id of the user sending the update request
278    * @return {@link Action} model object for the update action
279    * @throws ActionException Exception with an action library specific code, short description and
280    *                         detailed message for the error occurred for the error occurred during
281    *                         the operation
282    */
283   @Override
284   public Action updateAction(Action action, String user) throws ActionException {
285     try {
286       log.debug("entering updateAction to update action with invariantUuId = "
287           + action.getActionInvariantUuId() + " by user = " + user);
288       String invariantUuId = action.getActionInvariantUuId();
289       actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_API);
290       VersionInfo versionInfo = versioningManager
291           .getEntityVersionInfo(ACTION_VERSIONABLE_TYPE, invariantUuId, user,
292               VersionableEntityAction.Write);
293       actionLogPostProcessor(StatusCode.COMPLETE);
294       log.metrics("");
295
296       Version activeVersion = versionInfo.getActiveVersion();
297       validateActions(action, activeVersion);
298       action.setStatus(ActionStatus.Locked); //Status will be Checkout for update
299       updateData(action);
300       action.setUser(user);
301       action.setTimestamp(getCurrentTimeStampUtc());
302       actionDao.updateAction(action);
303
304     } catch (CoreException ce) {
305       formAndThrowException(ce);
306     }
307     log.debug("exit updateAction");
308     return action;
309   }
310
311   /**
312    * Checkout an existing action.
313    *
314    * @param invariantUuId actionInvariantUuId of the action to be checked out
315    * @param user          AT&T id of the user sending the checkout request
316    * @return {@link Action} model object for the checkout action
317    * @throws ActionException Exception with an action library specific code, short description and
318    *                         detailed message for the error occurred for the error occurred during
319    *                         the operation
320    */
321   @Override
322   public Action checkout(String invariantUuId, String user) throws ActionException {
323     Version version = null;
324     ActionEntity actionEntity = null;
325     try {
326       log.debug(
327           "entering checkout for Action with invariantUUID= " + invariantUuId + " by user = "
328               + user);
329       actionLogPreProcessor(ActionSubOperation.CHECKOUT_ACTION, TARGET_ENTITY_API);
330       version = versioningManager.checkout(ACTION_VERSIONABLE_TYPE, invariantUuId, user);
331       actionLogPostProcessor(StatusCode.COMPLETE);
332       log.metrics("");
333
334       actionEntity =
335           updateUniqueIdForVersion(invariantUuId, version, ActionStatus.Locked.name(), user);
336     } catch (CoreException exception) {
337       if (exception.code() != null && exception.code().id().equals(
338           VersioningErrorCodes.CHECKOT_ON_LOCKED_ENTITY)) {
339         actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
340         VersionInfoEntity versionInfoEntity =
341             versionInfoDao.get(new VersionInfoEntity(ACTION_VERSIONABLE_TYPE, invariantUuId));
342         actionLogPostProcessor(StatusCode.COMPLETE);
343         log.metrics("");
344         String checkoutUser = versionInfoEntity.getCandidate().getUser();
345         log.debug(
346             "Actual checkout user for Action with invariantUUID= " + invariantUuId + " is = "
347                 + checkoutUser);
348         if (!checkoutUser.equals(user)) {
349           throw new ActionException(ACTION_CHECKOUT_ON_LOCKED_ENTITY_OTHER_USER,
350               exception.getMessage());
351         }
352       }
353       formAndThrowException(exception);
354     }
355     log.debug(
356         "exit checkout for Action with invariantUUID= " + invariantUuId + " by user = " + user);
357     return actionEntity != null ? actionEntity.toDto() : new Action();
358   }
359
360   /**
361    * Undo an already checked out action.
362    *
363    * @param invariantUuId actionInvariantUuId of the checked out action
364    * @param user          AT&T id of the user sending the request
365    * @throws ActionException Exception with an action library specific code, short description and
366    *                         detailed message for the error occurred for the error occurred during
367    *                         the operation
368    */
369   @Override
370   public void undoCheckout(String invariantUuId, String user) throws ActionException {
371     Version version;
372     try {
373       log.debug(
374           "entering undoCheckout for Action with invariantUUID= " + invariantUuId + " by user = "
375               + user);
376
377       actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
378       //Get list of uploaded artifacts in this checked out version
379       VersionInfoEntity versionInfoEntity =
380           versionInfoDao.get(new VersionInfoEntity(ACTION_VERSIONABLE_TYPE, invariantUuId));
381       actionLogPostProcessor(StatusCode.COMPLETE);
382       log.metrics("");
383       if (versionInfoEntity == null) {
384         throw new CoreException(
385             new EntityNotExistErrorBuilder(ACTION_VERSIONABLE_TYPE, invariantUuId).build());
386       }
387       UserCandidateVersion candidate = versionInfoEntity.getCandidate();
388       Version activeVersion;
389       if (candidate != null) {
390         activeVersion = candidate.getVersion();
391       } else {
392         activeVersion = versionInfoEntity.getActiveVersion();
393       }
394
395       actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_VERSION, TARGET_ENTITY_DB);
396       Action action = actionDao.get(new ActionEntity(invariantUuId, activeVersion)).toDto();
397       actionLogPostProcessor(StatusCode.COMPLETE);
398       log.metrics("");
399
400       //Perform undo checkout on the action
401       actionLogPreProcessor(ActionSubOperation.UNDO_CHECKOUT_ACTION, TARGET_ENTITY_API);
402       version = versioningManager.undoCheckout(ACTION_VERSIONABLE_TYPE, invariantUuId, user);
403       actionLogPostProcessor(StatusCode.COMPLETE);
404       log.metrics("");
405
406       if (version.equals(new Version(0, 0))) {
407         actionLogPreProcessor(ActionSubOperation.DELETE_UNIQUEVALUE, TARGET_ENTITY_API);
408         UniqueValueUtil uniqueValueUtil =
409             new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface());
410         uniqueValueUtil
411             .deleteUniqueValue(ActionConstants.UniqueValues.ACTION_NAME, action.getName());
412         actionLogPostProcessor(StatusCode.COMPLETE);
413         log.metrics("");
414
415         actionLogPreProcessor(ActionSubOperation.DELETE_ACTIONVERSION, TARGET_ENTITY_DB);
416         //Added for the case where Create->Undo_Checkout->Checkout should not get the action
417         versionInfoDao.delete(new VersionInfoEntity(ACTION_VERSIONABLE_TYPE, invariantUuId));
418         actionLogPostProcessor(StatusCode.COMPLETE);
419         log.metrics("");
420       }
421
422       List<ActionArtifact> currentVersionArtifacts = action.getArtifacts();
423
424       //Delete the artifacts from action_artifact table (if any)
425       if (CollectionUtils.isNotEmpty(currentVersionArtifacts) &&
426           currentVersionArtifacts.size() > 0) {
427         for (ActionArtifact artifact : currentVersionArtifacts) {
428           ActionArtifactEntity artifactDeleteEntity =
429               new ActionArtifactEntity(artifact.getArtifactUuId(),
430                   getEffectiveVersion(activeVersion.toString()));
431           actionLogPreProcessor(ActionSubOperation.DELETE_ARTIFACT, TARGET_ENTITY_DB);
432           actionArtifactDao.delete(artifactDeleteEntity);
433           actionLogPostProcessor(StatusCode.COMPLETE);
434           log.metrics("");
435         }
436       }
437     } catch (CoreException exception) {
438       formAndThrowException(exception);
439     }
440     log.debug(
441         "exit undoCheckout for Action with invariantUUID= " + invariantUuId + " by user = " + user);
442   }
443
444   /**
445    * Checkin a checked out action.
446    *
447    * @param invariantUuId actionInvariantUuId of the checked out action
448    * @param user          AT&T id of the user sending the request
449    * @return {@link Action} model object for the updated action
450    * @throws ActionException Exception with an action library specific code, short description and
451    *                         detailed message for the error occurred for the error occurred during
452    *                         the operation
453    */
454   @Override
455   public Action checkin(String invariantUuId, String user) throws ActionException {
456     Version version = null;
457     ActionEntity actionEntity = null;
458     try {
459       log.debug("entering checkin for Action with invariantUUID= " + invariantUuId + " by user = "
460           + user);
461       actionLogPreProcessor(ActionSubOperation.CHECKIN_ACTION, TARGET_ENTITY_API);
462       version = versioningManager.checkin(ACTION_VERSIONABLE_TYPE, invariantUuId, user, null);
463       actionLogPostProcessor(StatusCode.COMPLETE);
464       log.metrics("");
465       actionEntity =
466           updateStatusForVersion(invariantUuId, version, ActionStatus.Available.name(), user);
467     } catch (CoreException exception) {
468       formAndThrowException(exception);
469     }
470     log.debug(
471         "exit checkin for Action with invariantUUID= " + invariantUuId + " by user = " + user);
472     return actionEntity != null ? actionEntity.toDto() : new Action();
473   }
474
475   /**
476    * Submit a checked in action.
477    *
478    * @param invariantUuId actionInvariantUuId of the checked in action
479    * @param user          AT&T id of the user sending the request
480    * @return {@link Action} model object for the updated action
481    * @throws ActionException Exception with an action library specific code, short description and
482    *                         detailed message for the error occurred for the error occurred during
483    *                         the operation
484    */
485   @Override
486   public Action submit(String invariantUuId, String user) throws ActionException {
487     Version version = null;
488     ActionEntity actionEntity = null;
489     try {
490       log.debug(
491           "entering submit for Action with invariantUUID= " + invariantUuId + " by user = " + user);
492       actionLogPreProcessor(ActionSubOperation.SUBMIT_ACTION, TARGET_ENTITY_API);
493       version = versioningManager.submit(ACTION_VERSIONABLE_TYPE, invariantUuId, user, null);
494       actionLogPostProcessor(StatusCode.COMPLETE);
495       log.metrics("");
496       actionEntity =
497           updateUniqueIdForVersion(invariantUuId, version, ActionStatus.Final.name(), user);
498     } catch (CoreException exception) {
499       formAndThrowException(exception);
500     }
501     log.debug("exit submit for Action with invariantUUID= " + invariantUuId + " by user = " + user);
502     return actionEntity != null ? actionEntity.toDto() : new Action();
503   }
504
505   /**
506    * Download an artifact of an action.
507    *
508    * @param artifactUuId {@link ActionArtifact} object representing the artifact and its metadata
509    * @param actionUuId   UUID of the action for which the artifact has to be downloaded
510    * @return downloaded action artifact object
511    */
512   @Override
513   public ActionArtifact downloadArtifact(String actionUuId, String artifactUuId)
514       throws ActionException {
515     log.debug(" entering downloadArtifact with actionUUID= " + actionUuId + " and artifactUUID= "
516         + artifactUuId);
517     Action action = actionDao.getActionsByActionUuId(actionUuId);
518     ActionArtifact actionArtifact;
519     if (action != null) {
520       MDC.put(SERVICE_INSTANCE_ID, action.getActionInvariantUuId());
521       List<ActionArtifact> artifacts = action.getArtifacts();
522       String actionVersion = action.getVersion();
523       int effectiveVersion = getEffectiveVersion(actionVersion);
524       ActionArtifact artifactMetadata =
525           getArtifactMetadataFromAction(artifacts, ARTIFACT_METADATA_ATTR_UUID, artifactUuId);
526       if (artifactMetadata != null) {
527         String artifactName = artifactMetadata.getArtifactName();
528         actionArtifact = actionArtifactDao.downloadArtifact(effectiveVersion, artifactUuId);
529         actionArtifact.setArtifactName(artifactName);
530
531       } else {
532         throw new ActionException(ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE,
533             ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
534       }
535     } else {
536       throw new ActionException(ActionErrorConstants.ACTION_ENTITY_NOT_EXIST_CODE,
537           ActionErrorConstants.ACTION_ENTITY_NOT_EXIST);
538     }
539     log.debug(" exit downloadArtifact with actionUUID= " + actionUuId + " and artifactUUID= "
540         + artifactUuId);
541     return actionArtifact;
542   }
543
544   /**
545    * Upload an artifact to an action.
546    *
547    * @param artifact            {@link ActionArtifact} object representing the artifact and its
548    *                            metadata
549    * @param actionInvariantUuId Invariant UUID of the action to which the artifact has to be
550    *                            uploaded
551    * @param user                User ID of the user sending the request
552    * @return Uploaded action artifact object
553    */
554   @Override
555   public ActionArtifact uploadArtifact(ActionArtifact artifact, String actionInvariantUuId,
556                                        String user) {
557     ActionArtifact uploadArtifactResponse = new ActionArtifact();
558     try {
559       log.debug("entering uploadArtifact with actionInvariantUuId= " + actionInvariantUuId
560           + "artifactName= " + artifact.getArtifactName());
561       actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_DB);
562       VersionInfo versionInfo = versioningManager
563           .getEntityVersionInfo(ACTION_VERSIONABLE_TYPE, actionInvariantUuId, user,
564               VersionableEntityAction.Write);
565       actionLogPostProcessor(StatusCode.COMPLETE);
566       log.metrics("");
567       Version activeVersion = versionInfo.getActiveVersion();
568       actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID, TARGET_ENTITY_DB);
569       Action action = actionDao.get(new ActionEntity(actionInvariantUuId, activeVersion)).toDto();
570       actionLogPostProcessor(StatusCode.COMPLETE);
571       log.metrics("");
572       String artifactUuId = generateActionArtifactUuId(action, artifact.getArtifactName());
573       //Check for Unique document name
574       List<ActionArtifact> actionArtifacts = action.getArtifacts();
575       ActionArtifact artifactMetadata =
576           getArtifactMetadataFromAction(actionArtifacts, ARTIFACT_METADATA_ATTR_NAME,
577               artifact.getArtifactName());
578       if (artifactMetadata != null) {
579         throw new ActionException(ACTION_ARTIFACT_ALREADY_EXISTS_CODE,
580             String.format(ACTION_ARTIFACT_ALREADY_EXISTS, actionInvariantUuId));
581       }
582
583       //Create the artifact
584       artifact.setArtifactUuId(artifactUuId);
585       artifact.setTimestamp(getCurrentTimeStampUtc());
586       artifact.setEffectiveVersion(getEffectiveVersion(activeVersion.toString()));
587       actionArtifactDao.uploadArtifact(artifact);
588
589       //Update the action data field and timestamp
590       addArtifactMetadataInActionData(action, artifact);
591
592       //Set the response object
593       uploadArtifactResponse.setArtifactUuId(artifact.getArtifactUuId());
594     } catch (CoreException ce) {
595       formAndThrowException(ce);
596     }
597     log.debug(
598         "exit uploadArtifact with actionInvariantUuId= " + actionInvariantUuId + "artifactName= "
599             + artifact.getArtifactName());
600     return uploadArtifactResponse;
601   }
602
603   @Override
604   public void deleteArtifact(String actionInvariantUuId, String artifactUuId, String user)
605       throws ActionException {
606     log.debug(
607         "enter deleteArtifact with actionInvariantUuId= " + actionInvariantUuId + "artifactUUID= "
608             + artifactUuId + " and user = " + user);
609     Action action = actionDao.getLockedAction(actionInvariantUuId, user);
610     List<ActionArtifact> actionArtifacts = action.getArtifacts();
611     ActionArtifact artifactMetadata =
612         getArtifactMetadataFromAction(actionArtifacts, ARTIFACT_METADATA_ATTR_UUID, artifactUuId);
613     if (artifactMetadata == null) {
614       throw new ActionException(ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE,
615           ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
616     }
617     if (artifactMetadata.getArtifactProtection().equals(ActionArtifactProtection.readOnly.name())) {
618       throw new ActionException(ACTION_ARTIFACT_DELETE_READ_ONLY,
619           ACTION_ARTIFACT_DELETE_READ_ONLY_MSG);
620     } else {
621
622       //Update action by removing artifact metadata
623       String jsonData = action.getData();
624       List<ActionArtifact> artifacts = action.getArtifacts();//action.getArtifacts();
625       ActionArtifact artifact = null;
626       Iterator<ActionArtifact> it = artifacts.iterator();
627       while (it.hasNext()) {
628         artifact = it.next();
629         String artifactId = artifact.getArtifactUuId();
630         if (artifactId.equals(artifactUuId)) {
631           it.remove();
632         }
633       }
634
635       Map dataMap = JsonUtil.json2Object(jsonData, LinkedHashMap.class);
636       dataMap.put("artifacts", artifacts);
637       String data = JsonUtil.object2Json(dataMap);
638       ActionEntity actionEntity = action.toEntity();
639       actionEntity.setData(data);
640       actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION, TARGET_ENTITY_DB);
641       actionDao.update(actionEntity);
642       actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
643       log.metrics("");
644       // delete Artifact if it's upload and delete action on same checkout version
645       String artifactName = artifactMetadata.getArtifactName();
646       String generatedArtifactUuId = generateActionArtifactUuId(action, artifactName);
647       if (generatedArtifactUuId.equals(artifactUuId)) {
648         if (artifact != null) {
649           ActionArtifactEntity artifactDeleteEntity =
650               new ActionArtifactEntity(artifact.getArtifactUuId(),
651                   getEffectiveVersion(action.getVersion()));
652           actionLogPreProcessor(ActionSubOperation.DELETE_ACTION_ARTIFACT, TARGET_ENTITY_DB);
653           actionArtifactDao.delete(artifactDeleteEntity);
654         }
655         actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
656         log.metrics("");
657       }
658
659     }
660     log.debug(
661         "exit deleteArtifact with actionInvariantUuId= " + actionInvariantUuId + "artifactUUID= "
662             + artifactUuId + " and user = " + user);
663   }
664
665   /**
666    * Update an existing artifact.
667    *
668    * @param artifact            {@link ActionArtifact} object representing the artifact and its
669    *                            metadata
670    * @param actionInvariantUuId Invariant UUID of the action to which the artifact has to be
671    *                            uploaded
672    * @param user                User ID of the user sending the request
673    */
674   public void updateArtifact(ActionArtifact artifact, String actionInvariantUuId, String user) {
675     try {
676       log.debug("Enter updateArtifact with actionInvariantUuId= " + actionInvariantUuId
677           + "artifactUUID= " + artifact.getArtifactUuId() + " and user = " + user);
678       actionLogPreProcessor(ActionSubOperation.GET_ACTION_VERSION, TARGET_ENTITY_API);
679       VersionInfo versionInfo = versioningManager
680           .getEntityVersionInfo(ACTION_VERSIONABLE_TYPE, actionInvariantUuId, user,
681               VersionableEntityAction.Write);
682       actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
683       log.metrics("");
684       Version activeVersion = versionInfo.getActiveVersion();
685       actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_ACTIONINVID, TARGET_ENTITY_DB);
686       Action action = actionDao.get(new ActionEntity(actionInvariantUuId, activeVersion)).toDto();
687       actionLogPostProcessor(StatusCode.COMPLETE, null, "", false);
688       log.metrics("");
689       List<ActionArtifact> actionArtifacts = action.getArtifacts();
690       ActionArtifact artifactMetadataByUuId =
691           getArtifactMetadataFromAction(actionArtifacts, ARTIFACT_METADATA_ATTR_UUID,
692               artifact.getArtifactUuId());
693       //Check if artifact is already in action or not
694       if (artifactMetadataByUuId == null) {
695         throw new ActionException(ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST_CODE,
696             ActionErrorConstants.ACTION_ARTIFACT_ENTITY_NOT_EXIST);
697       }
698       //If user tries to change artifact name
699       if (artifact.getArtifactName() != null
700           && !artifactMetadataByUuId.getArtifactName().equalsIgnoreCase(
701           artifact.getArtifactName())) {
702         throw new ActionException(ACTION_UPDATE_NOT_ALLOWED_CODE,
703             ACTION_ARTIFACT_UPDATE_NAME_INVALID);
704       }
705
706       byte[] payload = artifact.getArtifact();
707       String artifactLabel = artifact.getArtifactLabel();
708       String artifactCategory = artifact.getArtifactCategory();
709       String artifactDescription = artifact.getArtifactDescription();
710       String artifactProtection = artifact.getArtifactProtection();
711       String artifactName = artifact.getArtifactName();
712       //If artifact read only
713       if (artifactMetadataByUuId.getArtifactProtection()
714           .equals(ActionArtifactProtection.readOnly.name())) {
715         if (artifactName != null || artifactLabel != null || artifactCategory != null
716             || artifactDescription != null || payload != null) {
717           throw new ActionException(ACTION_ARTIFACT_UPDATE_READ_ONLY,
718               ACTION_ARTIFACT_UPDATE_READ_ONLY_MSG);
719         }
720         //Changing value from readOnly to readWrite
721         if (artifactProtection != null
722             && artifactProtection.equals(ActionArtifactProtection.readWrite.name())) {
723           artifactMetadataByUuId.setArtifactProtection(ActionArtifactProtection.readWrite.name());
724           artifactMetadataByUuId.setTimestamp(getCurrentTimeStampUtc());
725           updateArtifactMetadataInActionData(action, artifactMetadataByUuId);
726         }
727       } else {
728         int effectiveVersion = getEffectiveVersion(activeVersion.toString());
729         if (artifactLabel != null) {
730           artifactMetadataByUuId.setArtifactLabel(artifactLabel);
731         }
732         if (artifactCategory != null) {
733           artifactMetadataByUuId.setArtifactCategory(artifactCategory);
734         }
735         if (artifactDescription != null) {
736           artifactMetadataByUuId.setArtifactDescription(artifactDescription);
737         }
738         if (artifactProtection != null) {
739           artifactMetadataByUuId.setArtifactProtection(artifactProtection);
740         }
741         if (payload != null) {
742           //get artifact data from action_artifact table for updating the content
743           ActionArtifact artifactContent = new ActionArtifact();
744           artifactContent.setArtifactUuId(artifact.getArtifactUuId());
745           artifactContent.setArtifact(payload);
746           artifactContent.setEffectiveVersion(effectiveVersion);
747           actionArtifactDao.updateArtifact(artifactContent);
748         }
749         //Update the action data field and timestamp
750         artifactMetadataByUuId.setTimestamp(getCurrentTimeStampUtc());
751         updateArtifactMetadataInActionData(action, artifactMetadataByUuId);
752       }
753       log.debug("exit updateArtifact with actionInvariantUuId= " + actionInvariantUuId
754           + "artifactUUID= " + artifact.getArtifactUuId() + " and user = " + user);
755     } catch (CoreException coreException) {
756       formAndThrowException(coreException);
757     }
758   }
759
760   /**
761    * Generate artifact UUID at runtime using action name and effective version.
762    *
763    * @param action       {@link Action} for which the artifact is being uploaded/updated/downloaded
764    * @param artifactName Artifact name
765    * @return Generated UUID string
766    */
767   private String generateActionArtifactUuId(Action action, String artifactName) {
768     int effectiveVersion = getEffectiveVersion(action.getVersion());
769     //Upper case for maintaining case-insensitive behavior for the artifact names
770     String artifactUuIdString =
771         action.getName().toUpperCase() + effectiveVersion + artifactName.toUpperCase();
772     String generateArtifactUuId =
773         UUID.nameUUIDFromBytes((artifactUuIdString).getBytes()).toString();
774     String artifactUuId = generateArtifactUuId.replace("-", "");
775     return artifactUuId.toUpperCase();
776   }
777
778   /**
779    * Generate the effective action version for artifact operations.
780    *
781    * @param actionVersion Version of the action as a string
782    * @return Effective version to be used for artifact operations
783    */
784   private int getEffectiveVersion(String actionVersion) {
785     Version version = Version.valueOf(actionVersion);
786     return version.getMajor() * 10000 + version.getMinor();
787   }
788
789   /**
790    * Update the data field of the Action object with the modified/generated fields after an
791    * operation.
792    *
793    * @param action Action object whose data field has to be updated
794    * @return Updated {@link Action} object
795    */
796   private Action updateData(Action action) {
797     log.debug("entering updateData to update data json for action with actionuuid=  "
798         + action.getActionUuId());
799     Map<String, String> dataMap = new LinkedHashMap<>();
800     dataMap.put(ActionConstants.UNIQUE_ID, action.getActionUuId());
801     dataMap.put(ActionConstants.VERSION, action.getVersion());
802     dataMap.put(ActionConstants.INVARIANTUUID, action.getActionInvariantUuId());
803     dataMap.put(ActionConstants.STATUS, action.getStatus().name());
804
805     String data = action.getData();
806     Map<String, String> currentDataMap = JsonUtil.json2Object(data, LinkedHashMap.class);
807     dataMap.putAll(currentDataMap);
808     data = JsonUtil.object2Json(dataMap);
809     action.setData(data);
810     log.debug("exit updateData");
811     return action;
812   }
813
814   /**
815    * Method to add the artifact metadata in the data attribute of action table.
816    *
817    * @param action   Action to which artifact is uploaded
818    * @param artifact Uploaded artifact object
819    */
820   private void addArtifactMetadataInActionData(Action action, ActionArtifact artifact) {
821
822     ActionArtifact artifactMetadata = new ActionArtifact();
823     artifactMetadata.setArtifactUuId(artifact.getArtifactUuId());
824     artifactMetadata.setArtifactName(artifact.getArtifactName());
825     artifactMetadata.setArtifactProtection(artifact.getArtifactProtection());
826     artifactMetadata.setArtifactLabel(artifact.getArtifactLabel());
827     artifactMetadata.setArtifactDescription(artifact.getArtifactDescription());
828     artifactMetadata.setArtifactCategory(artifact.getArtifactCategory());
829     artifactMetadata.setTimestamp(artifact.getTimestamp());
830
831     List<ActionArtifact> actionArtifacts = action.getArtifacts();
832     if (actionArtifacts == null) {
833       actionArtifacts = new ArrayList<>();
834     }
835     actionArtifacts.add(artifactMetadata);
836     action.setArtifacts(actionArtifacts);
837     String currentData = action.getData();
838     Map<String, Object> currentDataMap = JsonUtil.json2Object(currentData, LinkedHashMap.class);
839     currentDataMap.put(ActionConstants.ARTIFACTS, actionArtifacts);
840     String updatedActionData = JsonUtil.object2Json(currentDataMap);
841     action.setData(updatedActionData);
842     action.setTimestamp(artifact.getTimestamp());
843     actionDao.updateAction(action);
844   }
845
846   /**
847    * Get a list of last major and last minor version (no candidate) of action from a list of
848    * actions.
849    *
850    * @param actions Exhaustive list of the action versions
851    * @return List {@link Action} of last major and last minor version (no candidate) of action from
852    * a list of actions
853    */
854   private List<Action> getMajorMinorVersionActions(List<Action> actions) {
855     log.debug(" entering getMajorMinorVersionActions for actions ");
856     List<Action> list = new LinkedList<>();
857     actionLogPreProcessor(ActionSubOperation.GET_VERSIONINFO_FOR_ALL_ACTIONS, TARGET_ENTITY_API);
858     Map<String, VersionInfo> actionVersionMap = versioningManager
859         .listEntitiesVersionInfo(ACTION_VERSIONABLE_TYPE, "", VersionableEntityAction.Read);
860     actionLogPostProcessor(StatusCode.COMPLETE);
861     log.metrics("");
862     for (Action action : actions) {
863       if (action.getStatus() == ActionStatus.Deleted) {
864         continue;
865       }
866       VersionInfo actionVersionInfo = actionVersionMap.get(action.getActionInvariantUuId());
867       if (actionVersionInfo.getActiveVersion() != null
868           && actionVersionInfo.getActiveVersion().equals(Version.valueOf(action.getVersion()))) {
869         list.add(action);
870       } else if (actionVersionInfo.getLatestFinalVersion() != null
871           && actionVersionInfo.getLatestFinalVersion().equals(Version.valueOf(action.getVersion()))
872           &&
873           !actionVersionInfo.getLatestFinalVersion().equals(actionVersionInfo.getActiveVersion())) {
874         list.add(action);
875       }
876     }
877     log.debug(" exit getMajorMinorVersionActions for actions ");
878     return list;
879   }
880
881   /**
882    * CoreException object wrapper from Version library to Action Library Exception.
883    *
884    * @param exception CoreException object from version library
885    */
886   private void formAndThrowException(CoreException exception) {
887     log.debug("entering formAndThrowException with input CoreException =" + exception.code().id()
888         + " " + exception.getMessage());
889     String errorDescription = exception.getMessage();
890     String errorCode = exception.code().id();
891     ActionException actionException = new ActionException();
892     switch (errorCode) {
893       case VersioningErrorCodes.VERSIONABLE_ENTITY_NOT_EXIST:
894         actionException.setErrorCode(ACTION_ENTITY_NOT_EXIST_CODE);
895         actionException.setDescription(ACTION_ENTITY_NOT_EXIST);
896         break;
897       case VersioningErrorCodes.CHECKOT_ON_LOCKED_ENTITY:
898         actionException.setErrorCode(ACTION_CHECKOUT_ON_LOCKED_ENTITY);
899         actionException.setDescription(errorDescription);
900         break;
901       case VersioningErrorCodes.CHECKIN_ON_UNLOCKED_ENTITY:
902         actionException.setErrorCode(ACTION_CHECKIN_ON_UNLOCKED_ENTITY);
903         actionException.setDescription(errorDescription);
904         break;
905       case VersioningErrorCodes.SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED:
906         actionException.setErrorCode(ACTION_SUBMIT_FINALIZED_ENTITY_NOT_ALLOWED);
907         actionException.setDescription(errorDescription);
908         break;
909       case VersioningErrorCodes.SUBMIT_LOCKED_ENTITY_NOT_ALLOWED:
910         actionException.setErrorCode(ACTION_SUBMIT_LOCKED_ENTITY_NOT_ALLOWED);
911         actionException.setDescription(errorDescription);
912         break;
913       case VersioningErrorCodes.UNDO_CHECKOUT_ON_UNLOCKED_ENTITY:
914         actionException.setErrorCode(ACTION_UNDO_CHECKOUT_ON_UNLOCKED_ENTITY);
915         actionException.setDescription(errorDescription);
916         break;
917       case VersioningErrorCodes.EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER:
918         actionException.setErrorCode(ACTION_EDIT_ON_ENTITY_LOCKED_BY_OTHER_USER);
919         actionException.setDescription(errorDescription.replace("edit", "updat"));
920         break;
921       case VersioningErrorCodes.CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER:
922         actionException.setErrorCode(ACTION_CHECKIN_ON_ENTITY_LOCKED_BY_OTHER_USER);
923         actionException.setDescription(errorDescription);
924         break;
925       case VersioningErrorCodes.UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER:
926         actionException.setErrorCode(ACTION_UNDO_CHECKOUT_ON_ENTITY_LOCKED_BY_OTHER_USER);
927         actionException.setDescription(errorDescription);
928         break;
929       case VersioningErrorCodes.EDIT_ON_UNLOCKED_ENTITY:
930         actionException.setErrorCode(ACTION_UPDATE_ON_UNLOCKED_ENTITY);
931         actionException.setDescription(errorDescription.replace("edit", "update"));
932         break;
933       case VersioningErrorCodes.DELETE_ON_LOCKED_ENTITY:
934         actionException.setErrorCode(ACTION_DELETE_ON_LOCKED_ENTITY_CODE);
935         actionException.setDescription(errorDescription);
936         break;
937       default:
938         actionException.setErrorCode(ACTION_INTERNAL_SERVER_ERR_CODE);
939         actionException.setDescription(exception.getMessage());
940
941     }
942     //Todo - Uncomment only if class to be added in ERROR Log
943     /*actionErrorLogProcessor(CategoryLogLevel.ERROR, actionException.getErrorCode(),
944     actionException.getDescription());
945     log.error("");*/
946     log.debug(
947         "exit formAndThrowException with ActionException =" + actionException.getErrorCode()
948             + " " + actionException.getDescription());
949     throw actionException;
950   }
951
952   /**
953    * Validates an action object for business layer validations before an update operation.
954    *
955    * @param action        Action object to be validated
956    * @param activeVersion Active version of the actoin object
957    */
958   private void validateActions(Action action, Version activeVersion) {
959     try {
960       //Set version if not already available in input request
961       //If version set in input compare it with version from DB
962       if (StringUtils.isEmpty(action.getVersion())) {
963         action.setVersion(activeVersion.toString());
964       } else {
965         if (!activeVersion.equals(Version.valueOf(action.getVersion()))) {
966           throw new ActionException(ACTION_UPDATE_INVALID_VERSION,
967               String.format(ACTION_REQUESTED_VERSION_INVALID, action.getVersion()));
968         }
969       }
970       String invariantUuId = action.getActionInvariantUuId();
971       Version version = Version.valueOf(action.getVersion());
972       Action existingAction = getActions(invariantUuId, version);
973       if (existingAction == null || existingAction.getActionInvariantUuId() == null) {
974         throw new ActionException(ACTION_ENTITY_NOT_EXIST_CODE, ACTION_ENTITY_NOT_EXIST);
975       }
976       List<String> invalidParameters = new LinkedList<>();
977       //Prevent update of name, version and id fields
978       if (!existingAction.getName().equals(action.getName())) {
979         throw new ActionException(ACTION_UPDATE_NOT_ALLOWED_CODE_NAME,
980             ACTION_UPDATE_NOT_ALLOWED_FOR_NAME);
981       }
982       if (!StringUtils.isEmpty(action.getActionUuId())
983           && !existingAction.getActionUuId().equals(action.getActionUuId())) {
984         invalidParameters.add(UNIQUE_ID);
985       }
986       if (action.getStatus() != null && (existingAction.getStatus() != action.getStatus())) {
987         invalidParameters.add(STATUS);
988       }
989
990       if (!invalidParameters.isEmpty()) {
991         throw new ActionException(ACTION_UPDATE_NOT_ALLOWED_CODE,
992             String.format(ACTION_UPDATE_PARAM_INVALID, StringUtils.join(invalidParameters, ", ")));
993       }
994       action.setActionUuId(existingAction.getActionUuId());
995     } catch (IllegalArgumentException iae) {
996       String message = iae.getMessage();
997       if (message == VERSION_STRING_VIOLATION_MSG) {
998         throw new ActionException(ACTION_UPDATE_NOT_ALLOWED_CODE, message);
999       } else {
1000         throw iae;
1001       }
1002     }
1003   }
1004
1005   /**
1006    * Get an action version entity object.
1007    *
1008    * @param invariantUuId Invariant UUID of the action
1009    * @param version       Version of the action
1010    * @return {@link ActionEntity} object of the action version
1011    */
1012   private ActionEntity getActionsEntityByVersion(String invariantUuId, Version version) {
1013     log.debug(
1014         "entering getActionsEntityByVersion with invariantUUID= " + invariantUuId + " and version"
1015             + version);
1016     ActionEntity entity = null;
1017     if (version != null) {
1018       actionLogPreProcessor(ActionSubOperation.GET_ACTIONENTITY_BY_VERSION, TARGET_ENTITY_DB);
1019       entity = actionDao.get(
1020           new ActionEntity(invariantUuId != null ? invariantUuId.toUpperCase() : null, version));
1021       actionLogPostProcessor(StatusCode.COMPLETE);
1022       log.metrics("");
1023     }
1024     log.debug(
1025         "exit getActionsEntityByVersion with invariantUuId= " + invariantUuId + " and version"
1026             + version);
1027     return entity;
1028   }
1029
1030   /**
1031    * Get an action version object.
1032    *
1033    * @param invariantUuId Invariant UUID of the action
1034    * @param version       Version of the action
1035    * @return {@link Action} object of the action version
1036    */
1037   private Action getActions(String invariantUuId, Version version) {
1038     ActionEntity actionEntity =
1039         getActionsEntityByVersion(invariantUuId != null ? invariantUuId.toUpperCase() : null,
1040             version);
1041     return actionEntity != null ? actionEntity.toDto() : new Action();
1042   }
1043
1044   /**
1045    * Create and set the Unique ID in for an action version row.
1046    *
1047    * @param invariantUuId Invariant UUID of the action
1048    * @param version       Version of the action
1049    * @param status        Status of the action
1050    * @param user          AT&T id of the user sending the request
1051    * @return {@link ActionEntity} object of the action version
1052    */
1053   private ActionEntity updateUniqueIdForVersion(String invariantUuId, Version version,
1054                                                 String status, String user) {
1055     log.debug(
1056         "entering updateUniqueIdForVersion to update action with invariantUuId= " + invariantUuId
1057             + " with version,status and user as ::" + version + " " + status + " " + user);
1058     //generate UUID AND update for newly created entity row
1059     ActionEntity actionEntity = getActionsEntityByVersion(invariantUuId, version);
1060     if (actionEntity != null) {
1061       log.debug("Found action to be updated");
1062       String data = actionEntity.getData();
1063       String uniqueId = CommonMethods.nextUuId();
1064       Map<String, String> dataMap = JsonUtil.json2Object(data, LinkedHashMap.class);
1065       dataMap.put(ActionConstants.UNIQUE_ID, uniqueId);
1066       dataMap.put(ActionConstants.VERSION, version.toString());
1067       dataMap.put(ActionConstants.STATUS, status);
1068       data = JsonUtil.object2Json(dataMap);
1069
1070       actionEntity.setData(data);
1071       actionEntity.setActionUuId(uniqueId);
1072       actionEntity.setStatus(status);
1073       actionEntity.setUser(user);
1074       actionEntity.setTimestamp(getCurrentTimeStampUtc());
1075       actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION, TARGET_ENTITY_DB);
1076       actionDao.update(actionEntity);
1077       actionLogPostProcessor(StatusCode.COMPLETE);
1078       log.metrics("");
1079     }
1080
1081     log.debug(
1082         "exit updateUniqueIdForVersion to update action with invariantUUID= " + invariantUuId);
1083     return actionEntity;
1084   }
1085
1086   /**
1087    * Set the status for an action version row.
1088    *
1089    * @param invariantUuId Invariant UUID of the action
1090    * @param version       Version of the action
1091    * @param status        Status of the action
1092    * @param user          AT&T id of the user sending the request
1093    * @return {@link ActionEntity} object of the action version
1094    */
1095   private ActionEntity updateStatusForVersion(String invariantUuId, Version version, String status,
1096                                               String user) {
1097     log.debug(
1098         "entering updateStatusForVersion with invariantUuId= " + invariantUuId + " and version"
1099             + version + " for updating status " + status + " by user " + user);
1100     ActionEntity actionEntity = getActionsEntityByVersion(invariantUuId, version);
1101     if (actionEntity != null) {
1102       String data = actionEntity.getData();
1103       Map<String, String> dataMap = JsonUtil.json2Object(data, LinkedHashMap.class);
1104       dataMap.put(ActionConstants.STATUS, status);
1105       data = JsonUtil.object2Json(dataMap);
1106       actionEntity.setData(data);
1107       actionEntity.setStatus(status);
1108       actionEntity.setUser(user);
1109       actionEntity.setTimestamp(getCurrentTimeStampUtc());
1110       actionLogPreProcessor(ActionSubOperation.UPDATE_ACTION, TARGET_ENTITY_DB);
1111       actionDao.update(actionEntity);
1112       actionLogPostProcessor(StatusCode.COMPLETE);
1113       log.metrics("");
1114     }
1115     log.debug("exit updateStatusForVersion with invariantUuId= " + invariantUuId + " and version"
1116         + version + " for updating status " + status + " by user " + user);
1117     return actionEntity;
1118
1119   }
1120
1121   /**
1122    * Gets an artifact from the action artifact metadata by artifact name.
1123    *
1124    * @param actionArtifactList  Action's existing artifact list
1125    * @param artifactFilterType  Search criteria for artifact in action artifact metadata
1126    * @param artifactFilterValue Value of Search parameter
1127    * @return Artifact metadata object if artifact is present in action and null otherwise
1128    */
1129   private ActionArtifact getArtifactMetadataFromAction(List<ActionArtifact> actionArtifactList,
1130                                                        String artifactFilterType,
1131                                                        String artifactFilterValue) {
1132     ActionArtifact artifact = null;
1133     if (actionArtifactList != null && !actionArtifactList.isEmpty()) {
1134       for (ActionArtifact entry : actionArtifactList) {
1135         switch (artifactFilterType) {
1136           case ARTIFACT_METADATA_ATTR_UUID:
1137             String artifactUuId = entry.getArtifactUuId();
1138             if (artifactUuId != null && artifactUuId.equals(artifactFilterValue)) {
1139               artifact = entry;
1140               break;
1141             }
1142             break;
1143           case ARTIFACT_METADATA_ATTR_NAME:
1144             String existingArtifactName = entry.getArtifactName().toLowerCase();
1145             if (existingArtifactName.equals(artifactFilterValue.toLowerCase())) {
1146               artifact = entry;
1147               break;
1148             }
1149             break;
1150           default:
1151         }
1152       }
1153     }
1154     return artifact;
1155   }
1156
1157   /**
1158    * Method to update the artifact metadata in the data attribute of action table.
1159    *
1160    * @param action          Action to which artifact is uploaded
1161    * @param updatedArtifact updated artifact object
1162    */
1163   private void updateArtifactMetadataInActionData(Action action, ActionArtifact updatedArtifact) {
1164     for (ActionArtifact entry : action.getArtifacts()) {
1165       if (entry.getArtifactUuId().equals(updatedArtifact.getArtifactUuId())) {
1166         entry.setArtifactLabel(updatedArtifact.getArtifactLabel());
1167         entry.setArtifactCategory(updatedArtifact.getArtifactCategory());
1168         entry.setArtifactDescription(updatedArtifact.getArtifactDescription());
1169         entry.setArtifactProtection(updatedArtifact.getArtifactProtection());
1170         entry.setTimestamp(updatedArtifact.getTimestamp());
1171         break;
1172       }
1173     }
1174     String data = action.getData();
1175     Map<String, Object> map = JsonUtil.json2Object(data, LinkedHashMap.class);
1176     map.put(ActionConstants.ARTIFACTS, action.getArtifacts());
1177     String updatedActionData = JsonUtil.object2Json(map);
1178     action.setData(updatedActionData);
1179     action.setTimestamp(updatedArtifact.getTimestamp());
1180     actionDao.updateAction(action);
1181   }
1182 }