be.bittich.dynaorm.maping.BasicColumnMapping.java Source code

Java tutorial

Introduction

Here is the source code for be.bittich.dynaorm.maping.BasicColumnMapping.java

Source

/*
 * Copyright 2014 Nordine.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package be.bittich.dynaorm.maping;

import be.bittich.dynaorm.annotation.MetaColumn;
import be.bittich.dynaorm.annotation.PrimaryKey;
import be.bittich.dynaorm.core.TableColumn;
import be.bittich.dynaorm.dialect.Dialect;
import java.lang.reflect.Field;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.collections4.KeyValue;
import org.apache.commons.collections4.keyvalue.DefaultKeyValue;
import static org.apache.commons.lang3.StringUtils.isEmpty;

/**
 *
 * @author Nordine Bittich
 */
public class BasicColumnMapping implements ColumnMapping {

    private static final long serialVersionUID = 8850960551168456580L;

    @Override
    public <T> Map<String, Field> mapToSQLColumns(T obj, TableColumn tableColumn) {
        //get the fields from the object
        Field[] fields = obj.getClass().getDeclaredFields();
        Set<String> columnsFromDB = tableColumn.getColumns().keySet();
        //map the column name with the field
        Map<String, Field> filteredFields = new HashMap();
        for (Field field : fields) {
            field.setAccessible(true);
            MetaColumn annotationX = field.getAnnotation(MetaColumn.class);
            //by default, columnName=field name
            String columnName = field.getName();
            if (annotationX != null && !isEmpty(annotationX.columnName())) {
                columnName = annotationX.columnName();
            }
            //map only the field present in the table
            if (columnsFromDB.contains(columnName)) {
                filteredFields.put(columnName, field);
            }
        }

        return filteredFields;
    }

    /**
     * do the mapping
     *
     * @throws java.lang.IllegalAccessException
     */
    @Override
    public <T> KeyValue<List<String>, List<String>> getColumnsValuesMap(T t, TableColumn tableColumn)
            throws IllegalAccessException {
        Map<String, Field> mapFields = this.mapToSQLColumns(t, tableColumn);
        List<String> columnNames = new LinkedList();
        List<String> values = new LinkedList();

        for (String columnName : mapFields.keySet()) {
            try {
                Field field = mapFields.get(columnName);
                field.setAccessible(true);
                PrimaryKey annotationPK = field.getAnnotation(PrimaryKey.class);
                //we don't add generated values to the request
                if (annotationPK == null || !annotationPK.autoGenerated()) {
                    MetaColumn metaColumn = field.getAnnotation(MetaColumn.class);
                    if (metaColumn != null) {
                        if (metaColumn.notNull() && field.get(t) == null) {
                            throw new NullPointerException(
                                    String.format("Column %s should not be null!", columnName));
                        }
                    }
                    Object fieldVal = field.get(t);
                    String valString = doFilterBeforeApplyToString(fieldVal);
                    values.add(valString);
                    columnNames.add(columnName);
                }

            } catch (IllegalArgumentException ex) {
                Logger.getLogger(BasicColumnMapping.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        return new DefaultKeyValue<List<String>, List<String>>(columnNames, values);

    }

    private String doFilterBeforeApplyToString(Object object) {
        String valString = object.toString();
        //check if it's a date
        if (object instanceof Date) {
            DateFormat sdf = new SimpleDateFormat(Dialect.DATE_FORMAT);
            valString = sdf.format((Date) object);
        }
        return valString;
    }
}