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.
16 package org.openecomp.sdcrests.mapping;
18 import org.openecomp.sdc.common.errors.CoreException;
19 import org.openecomp.sdc.common.errors.ErrorCode;
22 * Base class for all mapping classes. Mapping classes will perform data mapping from source object to target object Base class provides
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>
28 public abstract class MappingBase<S, T> {
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.
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>
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);
49 * This method is called before the <code>doMapping</code> method.
51 public void preMapping(final S source, T target) {
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.
59 * @param source - the source object.
60 * @param target - the target object.
62 public abstract void doMapping(final S source, T target);
65 * This method is called after the <code>doMapping</code> method.
67 public void postMapping(final S source, T target) {
72 * Creates the instance of the input class.
74 * @return <code>Object</code>
76 private Object instantiateTarget(final Class<?> clazz) {
78 return clazz.newInstance();
79 } catch (InstantiationException | IllegalAccessException exception) {
80 throw new CoreException((new ErrorCode.ErrorCodeBuilder()).withMessage(exception.getMessage()).build(), exception);