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