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;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
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
46 public abstract class MappingBase<S, T> {
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
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>
58 private final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass().getName());
60 public final T applyMapping(final S source, Class<T> clazz) {
61 T target = (T) instantiateTarget(clazz);
62 if (source == null || target == null) {
65 preMapping(source, target);
66 doMapping(source, target);
67 postMapping(source, target);
75 * This method is called before the <code>doMapping</code> method.
78 public void preMapping(final S source, T target) {
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.
86 * @param source - the source object.
87 * @param target - the target object.
90 public abstract void doMapping(final S source, T target);
93 * This method is called after the <code>doMapping</code> method.
96 public void postMapping(final S source, T target) {
100 * Creates the instance of the input class.
102 * @return <code>Object</code>
105 private Object instantiateTarget(final Class<?> clazz) {
106 Object object = null;
108 object = clazz.newInstance();
110 } catch (InstantiationException | IllegalAccessException exception ) {
112 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(
113 LoggerConstants.TARGET_ENTITY,
114 LoggerServiceName.Create_LIMIT.toString(), ErrorLevel.ERROR.name(),
115 exception.getMessage(), exception.getMessage());
117 throw new CoreException((new ErrorCode.ErrorCodeBuilder())
118 .withMessage(exception.getMessage())
119 .withId(exception.getMessage())
120 .withCategory(ErrorCategory.APPLICATION).build());