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