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