Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.eu.evaluation.server.service.impl; import com.eu.evaluate.utils.BeanUtils; import com.eu.evaluation.anno.Dictinary; import com.eu.evaluation.anno.ForeignKey; import com.eu.evaluation.model.EntityEnum; import com.eu.evaluation.model.EvaluatedData; import com.eu.evaluation.model.dictionary.FieldDictionary; import com.eu.evaluation.model.dictionary.ObjectDictionary; import com.eu.evaluation.model.dictionary.ObjectRelation; import com.eu.evaluation.server.dao.dictionary.FieldDictionaryDAO; import com.eu.evaluation.server.dao.dictionary.ObjectDictionaryDAO; import com.eu.evaluation.server.dao.dictionary.ObjectRelationDAO; import com.eu.evaluation.server.service.DictionaryService; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Transient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * * @author dell */ @Component public class DictionaryServiceImpl implements DictionaryService { protected Log logger = LogFactory.getLog(getClass()); @Autowired private ObjectDictionaryDAO objectDictionaryDAO; @Autowired private FieldDictionaryDAO fieldDictionaryDAO; @Autowired private ObjectRelationDAO objectRelationDAO; public ObjectDictionary findObjectDictionary(String id) { return objectDictionaryDAO.get(id); } public FieldDictionary findFielddDictionary(String id) { return fieldDictionaryDAO.get(id); } public void initDirectionary() { //?? objectRelationDAO.removeAll(); objectDictionaryDAO.invalidateAll(); fieldDictionaryDAO.invalidateAll(); int i = 0; for (EntityEnum ee : EntityEnum.values()) { if (ee.getEntityClass() == null) { continue; } logger.debug("??" + ee.getName()); Entity entity = ee.getEntityClass().getAnnotation(Entity.class); if (entity == null) { logger.warn("EntityEnum " + ee.getName() + " " + ee.getEntityClass().getName() + " @Entity??"); continue; } Table table = ee.getEntityClass().getAnnotation(Table.class); String tableName = ee.getEntityClass().getSimpleName(); if (table != null) { tableName = table.name(); } ObjectDictionary od = objectDictionaryDAO.findByInstanceType(ee.getInstanceType()); if (od == null) { od = new ObjectDictionary(); } od.setDisplayname(ee.getName()); od.setInstanceClass(ee.getEntityClass().getName()); od.setInstanceType(ee.getInstanceType()); od.setSerial(i++); od.setValid(true); od.setTableName(tableName); objectDictionaryDAO.save(od); initField(ee, od); logger.debug("???" + ee.getName()); } } private void initField(EntityEnum ee, ObjectDictionary od) { Field[] fields = BeanUtils.getDeclaredFields(ee.getEntityClass(), EvaluatedData.class); List<FieldDictionary> fdList = new ArrayList<FieldDictionary>(); List<ObjectRelation> foreignList = new ArrayList<ObjectRelation>(); for (Field f : fields) { try { Method method = BeanUtils.getReadMethod(ee.getEntityClass(), f); Transient t = method.getAnnotation(Transient.class); if (t != null) {//???? logger.warn("??\n " + ee.getName() + " " + f.getName() + " Transient "); continue; } Dictinary dictinary = method.getAnnotation(Dictinary.class); if (dictinary == null) {//??? logger.warn("??\n " + ee.getName() + " " + f.getName() + " Dictinary "); continue; } FieldDictionary fd = fieldDictionaryDAO.findByObjectAndProperty(od.getId(), f.getName()); fd = fd == null ? new FieldDictionary() : fd; fd.setObjectDictionary(od);// fd.setPropertyName(f.getName());//?? fd.setValid(true);//? fd.setDisplayname(dictinary.displayname());//?? fd.setVisible(dictinary.visible());//??? fd.setSimpleProperty(BeanUtils.isSimpleTypeField(f));//?? Column column = method.getAnnotation(Column.class); fd.setFieldName(column != null ? column.name() : fd.getPropertyName());//??? //? if (dictinary.foreignKey() != null && dictinary.foreignKey().foreignClass() != Object.class) { ForeignKey foreignKey = dictinary.foreignKey(); ObjectRelation or = new ObjectRelation(); or.setSelfClass(od.getInstanceClass()); or.setPropertyName(fd.getPropertyName()); or.setRelationClass(foreignKey.foreignClass().getName()); or.setSimpleProperty(fd.isSimpleProperty()); foreignList.add(or); } fdList.add(fd); } catch (NoSuchMethodException ex) { logger.warn("??\n " + ee.getName() + " " + f.getName() + " read " + ex.getMessage()); } } fieldDictionaryDAO.save(fdList); objectRelationDAO.save(foreignList); } public ObjectDictionary findObjectDictionaryByName(String displayname) { return objectDictionaryDAO.findByDisplayName(displayname); } public FieldDictionary findFielddDictionaryByName(String objectDictnaryName, String propertyName) { return fieldDictionaryDAO.findByObjectNameAndProperty(objectDictnaryName, propertyName); } public List<FieldDictionary> findFieldDictionaryByInstanceType(int instanceType) { return fieldDictionaryDAO.findByInstanceType(instanceType); } }