All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ars.module.educate.service.AbstractClassesService Maven / Gradle / Ivy

The newest version!
package ars.module.educate.service;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.LinkedHashMap;

import ars.util.SimpleTree;
import ars.invoke.request.Requester;
import ars.invoke.request.ParameterInvalidException;
import ars.module.people.model.Group;
import ars.module.educate.model.Classes;
import ars.module.educate.model.Profession;
import ars.module.educate.service.ClassesService;
import ars.database.repository.Repositories;
import ars.database.service.StandardGeneralService;

/**
 * 教学班级业务操作抽象实现
 * 
 * @author yongqiangwu
 * 
 * @param 
 *            数据模型
 */
public abstract class AbstractClassesService extends StandardGeneralService
		implements ClassesService {

	@Override
	public void initObject(Requester requester, T entity) {
		super.initObject(requester, entity);
		boolean exists = this.getRepository().query().eq("profession", entity.getProfession())
				.eq("name", entity.getName()).ne("id", entity.getId()).count() > 0;
		if (exists) {
			throw new ParameterInvalidException("name", "exist");
		}
	}

	@Override
	public List trees(Requester requester, Integer level) {
		List objects = this.getQuery(requester).custom(requester.getParameters()).list();
		if (objects.isEmpty()) {
			return new ArrayList(0);
		}
		List departments = new LinkedList();
		Map> leaves = new LinkedHashMap>();
		Map professions = new LinkedHashMap();
		for (int i = 0; i < objects.size(); i++) {
			T classes = objects.get(i);
			Profession profession = classes.getProfession();
			Group department = profession.getDepartment();
			SimpleTree parent = professions.get(profession.getId());
			if (parent == null) {
				parent = Repositories.getSimpleTree(profession);
				professions.put(profession.getId(), parent);
				List classeses = leaves.get(department);
				if (classeses == null) {
					classeses = new LinkedList();
					leaves.put(department, classeses);
				}
				classeses.add(parent);
			}
			parent.getChildren().add(Repositories.getSimpleTree(classes));
			while (department != null && (level == null || department.getLevel() >= level)
					&& !departments.contains(department)) {
				departments.add(department);
				department = department.getParent();
			}
		}
		Map mappings = new HashMap(departments.size());
		List trunks = Repositories.getSimpleTrees(Repositories.mergeTrees(departments), mappings);
		for (Entry> entry : leaves.entrySet()) {
			SimpleTree leaf = mappings.get(entry.getKey());
			List children = entry.getValue();
			leaf.getChildren().addAll(0, children);
			for (SimpleTree child : children) {
				child.setParent(leaf);
			}
		}
		return trunks;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy