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