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