Merge "Replace FileUtils.toByteArray() with IOUtils"
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / openecomp-sdc-common-rest / src / main / java / org / openecomp / sdcrests / mapping / MappingBase.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.sdcrests.mapping;
22
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.common.errors.ErrorCategory;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerServiceName;
31
32 /**
33  * Base class for all mapping classes. Mapping classes will perform data mapping from source object
34  *  to target object Base class provides following<br>  <ol>  <li>provides life cycle of
35  * mapping class , first mapSimpleProperties is called and then  mapComplexProperties is
36  * called.</li>  <li>methods mapSimpleProperties and mapComplexProperties with default
37  * implementation, these should  be overridden by concrete mapping classes for writing mapping
38  * logic.</li>  </ol>
39  *
40  *
41  */
42
43 public abstract class MappingBase<S, T> {
44
45   /**
46    * Method is called for starting mapping from source object to target object method sets context
47    *  in the thread locale and than calls mapSimpleProperties and mapComplexProperties
48    * respectively.
49    *
50    * @param source : source object for mapping
51    * @param clazz  : target <code>Class</code> for mapping
52    * @return <code>T</code> - instance of type <code>T</code>
53    */
54
55   public final T applyMapping(final S source, Class<T> clazz) {
56     T target = (T) instantiateTarget(clazz);
57     if (source == null || target == null) {
58       //TODO: what what?
59     } else {
60       preMapping(source, target);
61       doMapping(source, target);
62       postMapping(source, target);
63
64     }
65     return target;
66
67   }
68
69   /**
70    * This method is called before the <code>doMapping</code> method.
71    */
72
73   public void preMapping(final S source, T target) {
74   }
75
76   /**
77    * The actual method that does the mapping between the <code>source</code> to <code>target</code>
78    * objects.  This method is being called automatically as part of the mapper class.  This
79    * method must be override (it is abstract) by the mapper class.
80    *
81    * @param source - the source object.
82    * @param target - the target object.
83    */
84
85   public abstract void doMapping(final S source, T target);
86
87   /**
88    * This method is called after the <code>doMapping</code> method.
89    */
90
91   public void postMapping(final S source, T target) {
92   }
93
94   /**
95    * Creates the instance of the input class.
96    *
97    * @return <code>Object</code>
98    */
99
100   private Object instantiateTarget(final Class<?> clazz) {
101     Object object = null;
102     try {
103       object = clazz.newInstance();
104
105     } catch (InstantiationException | IllegalAccessException exception ) {
106       //TODO: what what?
107       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(
108           LoggerConstants.TARGET_ENTITY,
109           LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
110           exception.getMessage(), exception.getMessage());
111
112       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
113           .withMessage(exception.getMessage())
114           .withId(exception.getMessage())
115           .withCategory(ErrorCategory.APPLICATION).build());
116
117
118     }
119     return object;
120
121   }
122
123 }
124