[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / errors / UploadInvalidErrorBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.vendorsoftwareproduct.errors;
22
23 import org.openecomp.sdc.common.errors.BaseErrorBuilder;
24 import org.openecomp.sdc.common.errors.ErrorCategory;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27
28 import java.util.List;
29 import java.util.Map;
30
31 public class UploadInvalidErrorBuilder extends BaseErrorBuilder {
32   private static final String UPLOAD_INVALID_DETAILED_MSG =
33       "File uploaded for vendor software product with Id %s and version %s is invalid: %s";
34   private static final String UPLOAD_INVALID_MSG = "Uploaded file is invalid";
35
36   /**
37    * Instantiates a new Upload invalid error builder.
38    *
39    * @param vendorSoftwareProductId the vendor software product id
40    * @param version                 the version
41    * @param errors                  the errors
42    */
43   public UploadInvalidErrorBuilder(String vendorSoftwareProductId, Version version,
44                                    Map<String, List<ErrorMessage>> errors) {
45     getErrorCodeBuilder().withId(VendorSoftwareProductErrorCodes.UPLOAD_INVALID);
46     getErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION);
47     getErrorCodeBuilder().withMessage(String
48         .format(UPLOAD_INVALID_DETAILED_MSG, vendorSoftwareProductId, version.toString(),
49             toString(errors)));
50   }
51
52   /**
53    * Instantiates a new Upload invalid error builder.
54    */
55   public UploadInvalidErrorBuilder() {
56     getErrorCodeBuilder().withId(VendorSoftwareProductErrorCodes.UPLOAD_INVALID);
57     getErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION);
58     getErrorCodeBuilder().withMessage(UPLOAD_INVALID_MSG);
59   }
60
61   private String toString(Map<String, List<ErrorMessage>> errors) {
62     StringBuffer sb = new StringBuffer();
63     errors.entrySet().stream()
64         .forEach(entry -> singleErrorToString(sb, entry.getKey(), entry.getValue()));
65     return sb.toString();
66   }
67
68   private void singleErrorToString(StringBuffer sb, String fileName, List<ErrorMessage> errors) {
69     sb.append(System.lineSeparator());
70     sb.append(fileName);
71     sb.append(sb.append(": "));
72     errors.stream().forEach(
73         error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("], "));
74   }
75
76 }