org.beangle.model.transfer.importer.MultiEntityImporter.java Source code

Java tutorial

Introduction

Here is the source code for org.beangle.model.transfer.importer.MultiEntityImporter.java

Source

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.model.transfer.importer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.model.Entity;
import org.beangle.model.entity.Model;
import org.beangle.model.entity.ObjectAndType;
import org.beangle.model.entity.Populator;
import org.beangle.model.entity.types.EntityType;
import org.beangle.model.transfer.TransferMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MultiEntityImporter extends AbstractItemImporter implements EntityImporter {

    private final Logger logger = LoggerFactory.getLogger(MultiEntityImporter.class);

    protected Map<String, Object> current = CollectUtils.newHashMap();

    protected Set<String> foreignerKeys = CollectUtils.newHashSet();

    protected Populator populator = Model.getPopulator();

    // [alias,entityType]
    protected Map<String, EntityType> entityTypes = new HashMap<String, EntityType>();

    public MultiEntityImporter() {
        super();
        foreignerKeys.add("code");
    }

    public void transferItem() {
        if (logger.isDebugEnabled()) {
            logger.debug("tranfer index:" + getTranferIndex() + ":" + ArrayUtils.toString(values));
        }
        // 
        for (int i = 0; i < attrs.length; i++) {
            Object value = values.get(attrs[i]);
            // 
            if (StringUtils.isBlank(attrs[i]))
                continue;
            // ?trim
            if (value instanceof String) {
                String strValue = (String) value;
                if (StringUtils.isBlank(strValue)) {
                    value = null;
                } else {
                    value = StringUtils.trim(strValue);
                }
            }
            // ?null
            if (null == value) {
                continue;
            } else {
                if (value.equals(Model.NULL)) {
                    value = null;
                }
            }
            Object entity = getCurrent(attrs[i]);
            String attr = processAttr(attrs[i]);
            String entityName = getEntityName(attrs[i]);
            // 
            if (StringUtils.contains(attr, '.')) {
                if (null != foreignerKeys) {
                    boolean isForeigner = isForeigner(attr);
                    // ,?parentPath?
                    // ,?.
                    if (isForeigner) {
                        String parentPath = StringUtils.substringBeforeLast(attr, ".");
                        ObjectAndType propertyType = populator.initProperty(entity, entityName, parentPath);
                        Object property = propertyType.getObj();
                        if (property instanceof Entity<?>) {
                            if (((Entity<?>) property).isPersisted()) {
                                populator.populateValue(entity, parentPath, null);
                                populator.initProperty(entity, entityName, parentPath);
                            }
                        }
                    }
                }
            }
            populator.populateValue(entity, attr, value);
        }
    }

    public String processAttr(String attr) {
        return StringUtils.substringAfter(attr, ".");
    }

    protected Class<?> getEntityClass(String attr) {
        return getEntityType(attr).getEntityClass();
    }

    protected EntityType getEntityType(String attr) {
        String alias = StringUtils.substringBefore(attr, ".");
        EntityType entityType = (EntityType) entityTypes.get(alias);
        if (null == entityType) {
            entityType = (EntityType) entityTypes.get(attr);
        }
        return entityType;
    }

    public void addEntity(String alias, Class<?> entityClass) {
        EntityType entityType = Model.getEntityType(entityClass);
        if (null == entityType) {
            throw new RuntimeException("cannot find entity type for " + entityClass);
        }
        entityTypes.put(alias, entityType);
    }

    public void addEntity(String alias, String entityName) {
        EntityType entityType = Model.getEntityType(entityName);
        if (null == entityType) {
            throw new RuntimeException("cannot find entity type for " + entityName);
        }
        entityTypes.put(alias, entityType);
    }

    protected String getEntityName(String attr) {
        return getEntityType(attr).getEntityName();
    }

    public Object getCurrent(String attr) {
        String alias = StringUtils.substringBefore(attr, ".");
        Object entity = current.get(alias);
        if (null == entity) {
            EntityType entityType = (EntityType) entityTypes.get(alias);
            if (null == entityType) {
                logger.error("Not register entity type for {}", alias);
                throw new RuntimeException("Not register entity type for " + alias);
            } else {
                entity = entityType.newInstance();
                current.put(alias, entity);
                return entity;
            }
        }
        return entity;
    }

    /**
     * FIXME
     */
    public void beforeImport() {
        // ?
        // super.beforeImport();
        // ??
        List<String> errorAttrs = checkAttrs();
        if (!errorAttrs.isEmpty()) {
            transferResult.addFailure(TransferMessage.ERROR_ATTRS, errorAttrs.toString());
            throw new RuntimeException("error attrs:" + errorAttrs);
        }
    }

    /**
     * ???? TODO ?name
     * 
     * @return
     */
    protected List<String> checkAttrs() {
        List<String> errorAttrs = CollectUtils.newArrayList();
        List<String> rightAttrs = CollectUtils.newArrayList();
        for (int i = 0; i < attrs.length; i++) {
            if (StringUtils.isBlank(attrs[i])) {
                continue;
            }
            try {
                EntityType entityType = getEntityType(attrs[i]);
                Entity<?> example = (Entity<?>) entityType.newInstance();
                String entityName = entityType.getEntityName();
                String attr = processAttr(attrs[i]);
                if (attr.indexOf('.') > -1) {
                    populator.initProperty(example, entityName, StringUtils.substringBeforeLast(attr, "."));
                }
                rightAttrs.add(attrs[i]);
            } catch (Exception e) {
                errorAttrs.add(attrs[i]);
            }
        }

        attrs = new String[rightAttrs.size()];
        rightAttrs.toArray(attrs);
        return errorAttrs;
    }

    public Object getCurrent() {
        return current;
    }

    @SuppressWarnings("unchecked")
    public void setCurrent(Object object) {
        this.current = (Map<String, Object>) object;
    }

    public String getDataName() {
        return "multi entity";
    }

    public void beforeImportItem() {
        this.current = CollectUtils.newHashMap();
    }

    private boolean isForeigner(String attr) {
        String property = StringUtils.substringAfterLast(attr, ".");
        return foreignerKeys.contains(property);
    }

    public Set<String> getForeignerKeys() {
        return foreignerKeys;
    }

    public void setForeignerKeys(Set<String> foreignerKeys) {
        this.foreignerKeys = foreignerKeys;
    }

    public void addForeignedKeys(String foreignerKey) {
        foreignerKeys.add(foreignerKey);
    }

    public void setPopulator(Populator populator) {
        this.populator = populator;
    }

    public Map<String, EntityType> getEntityTypes() {
        return entityTypes;
    }

    public void setEntityTypes(Map<String, EntityType> entityTypes) {
        this.entityTypes = entityTypes;
    }

}