2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.mapping;
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;
33 * Base class for all mapping classes. Mapping classes will perform data mapping from source object
34 * to target object Base class provides following<br> <ol> <li>provides life cycle of
35 * mapping class , first mapSimpleProperties is called and then mapComplexProperties is
36 * called.</li> <li>methods mapSimpleProperties and mapComplexProperties with default
37 * implementation, these should be overridden by concrete mapping classes for writing mapping
43 public abstract class MappingBase<S, T> {
46 * Method is called for starting mapping from source object to target object method sets context
47 * in the thread locale and than calls mapSimpleProperties and mapComplexProperties
50 * @param source : source object for mapping
51 * @param clazz : target <code>Class</code> for mapping
52 * @return <code>T</code> - instance of type <code>T</code>
55 public final T applyMapping(final S source, Class<T> clazz) {
56 T target = (T) instantiateTarget(clazz);
57 if (source == null || target == null) {
60 preMapping(source, target);
61 doMapping(source, target);
62 postMapping(source, target);
70 * This method is called before the <code>doMapping</code> method.
73 public void preMapping(final S source, T target) {
77 * The actual method that does the mapping between the <code>source</code> to <code>target</code>
78 * objects. This method is being called automatically as part of the mapper class. This
79 * method must be override (it is abstract) by the mapper class.
81 * @param source - the source object.
82 * @param target - the target object.
85 public abstract void doMapping(final S source, T target);
88 * This method is called after the <code>doMapping</code> method.
91 public void postMapping(final S source, T target) {
95 * Creates the instance of the input class.
97 * @return <code>Object</code>
100 private Object instantiateTarget(final Class<?> clazz) {
101 Object object = null;
103 object = clazz.newInstance();
105 } catch (InstantiationException | IllegalAccessException exception ) {
107 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(
108 LoggerConstants.TARGET_ENTITY,
109 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
110 exception.getMessage(), exception.getMessage());
112 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
113 .withMessage(exception.getMessage())
114 .withId(exception.getMessage())
115 .withCategory(ErrorCategory.APPLICATION).build());