8cf985feb6588c92fb423d96edc2402e85a68fda
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / component / ComponentDescriptionValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.validation.component;
21
22 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
23 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.openecomp.sdc.common.util.ValidationUtils;
32 import org.openecomp.sdc.exception.ResponseFormat;
33
34 @org.springframework.stereotype.Component
35 public class ComponentDescriptionValidator implements ComponentFieldValidator {
36
37     private static final Logger log = Logger.getLogger(ComponentTagsValidator.class.getName());
38     private ComponentsUtils componentsUtils;
39
40     public ComponentDescriptionValidator(ComponentsUtils componentsUtils) {
41         this.componentsUtils = componentsUtils;
42     }
43
44     @Override
45     public void validateAndCorrectField(User user, Component component, AuditingActionEnum actionEnum) {
46         ComponentTypeEnum type = component.getComponentType();
47         String description = component.getDescription();
48         if (!ValidationUtils.validateStringNotEmpty(description)) {
49             auditErrorAndThrow(user, component, actionEnum, ActionStatus.COMPONENT_MISSING_DESCRIPTION);
50         }
51         description = ValidationUtils.cleanUpText(description);
52         try {
53             validateComponentDescription(description, type);
54         } catch (ComponentException e) {
55             ResponseFormat errorResponse = componentsUtils.getResponseFormat(e.getActionStatus(), component.getComponentType().getValue());
56             componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
57             throw e;
58         }
59         component.setDescription(description);
60     }
61
62     private void auditErrorAndThrow(User user, org.openecomp.sdc.be.model.Component component, AuditingActionEnum actionEnum,
63                                     ActionStatus actionStatus) {
64         ResponseFormat errorResponse = componentsUtils.getResponseFormat(actionStatus, component.getComponentType().getValue());
65         componentsUtils.auditComponentAdmin(errorResponse, user, component, actionEnum, component.getComponentType());
66         throw new ByActionStatusComponentException(actionStatus, component.getComponentType().getValue());
67     }
68
69     private void validateComponentDescription(String description, ComponentTypeEnum type) {
70         if (description != null) {
71             if (!ValidationUtils.validateDescriptionLength(description)) {
72                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_DESCRIPTION_EXCEEDS_LIMIT, type.getValue(),
73                     "" + ValidationUtils.COMPONENT_DESCRIPTION_MAX_LENGTH);
74             }
75             if (!ValidationUtils.validateCommentPattern(description)) {
76                 throw new ByActionStatusComponentException(ActionStatus.COMPONENT_INVALID_DESCRIPTION, type.getValue());
77             }
78         }
79     }
80 }