Removed MDC from four modules
[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.ErrorCode;
21
22 /**
23  * Base class for all mapping classes. Mapping classes will perform data mapping from source object
24  *  to target object Base class provides following<br>  <ol>  <li>provides life cycle of
25  * mapping class , first mapSimpleProperties is called and then  mapComplexProperties is
26  * called.</li>  <li>methods mapSimpleProperties and mapComplexProperties with default
27  * implementation, these should  be overridden by concrete mapping classes for writing mapping
28  * logic.</li>  </ol>
29  *
30  *
31  */
32
33 public abstract class MappingBase<S, T> {
34
35   /**
36    * Method is called for starting mapping from source object to target object method sets context
37    *  in the thread locale and than calls mapSimpleProperties and mapComplexProperties
38    * respectively.
39    *
40    * @param source : source object for mapping
41    * @param clazz  : target <code>Class</code> for mapping
42    * @return <code>T</code> - instance of type <code>T</code>
43    */
44
45   public final T applyMapping(final S source, Class<T> clazz) {
46     T target = (T) instantiateTarget(clazz);
47     if (source != null && target != null) {
48       preMapping(source, target);
49       doMapping(source, target);
50       postMapping(source, target);
51
52     }
53     return target;
54
55   }
56
57   /**
58    * This method is called before the <code>doMapping</code> method.
59    */
60   public void preMapping(final S source, T target) {
61     // extension point
62   }
63
64   /**
65    * The actual method that does the mapping between the <code>source</code> to <code>target</code>
66    * objects.  This method is being called automatically as part of the mapper class.  This
67    * method must be override (it is abstract) by the mapper class.
68    *
69    * @param source - the source object.
70    * @param target - the target object.
71    */
72
73   public abstract void doMapping(final S source, T target);
74
75   /**
76    * This method is called after the <code>doMapping</code> method.
77    */
78   public void postMapping(final S source, T target) {
79     // extension point
80   }
81
82   /**
83    * Creates the instance of the input class.
84    *
85    * @return <code>Object</code>
86    */
87
88   private Object instantiateTarget(final Class<?> clazz) {
89
90     try {
91       return clazz.newInstance();
92     } catch (InstantiationException | IllegalAccessException exception ) {
93       throw new CoreException((new ErrorCode.ErrorCodeBuilder())
94           .withMessage(exception.getMessage()).build(), exception);
95     }
96   }
97 }
98