2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdcrests.mapping;
19 import org.openecomp.sdc.common.errors.CoreException;
20 import org.openecomp.sdc.common.errors.ErrorCode;
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
33 public abstract class MappingBase<S, T> {
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
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>
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);
58 * This method is called before the <code>doMapping</code> method.
60 public void preMapping(final S source, T target) {
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.
69 * @param source - the source object.
70 * @param target - the target object.
73 public abstract void doMapping(final S source, T target);
76 * This method is called after the <code>doMapping</code> method.
78 public void postMapping(final S source, T target) {
83 * Creates the instance of the input class.
85 * @return <code>Object</code>
88 private Object instantiateTarget(final Class<?> clazz) {
91 return clazz.newInstance();
92 } catch (InstantiationException | IllegalAccessException exception ) {
93 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
94 .withMessage(exception.getMessage()).build(), exception);