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