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.dao.eva; import com.eu.evaluation.model.dictionary.FieldDictionary; import com.eu.evaluation.model.dictionary.ObjectDictionary; import com.eu.evaluation.model.eva.EvaluateItemTemplate; import com.eu.evaluation.model.eva.EvaluateTypeEnum; import com.eu.evaluation.server.dao.AbstractDAO; import java.util.List; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.stereotype.Repository; /** * * @author dell */ @Repository public class EvaluateItemTemplateDAO extends AbstractDAO<EvaluateItemTemplate> { public EvaluateItemTemplate findTheMatching(EvaluateTypeEnum type, ObjectDictionary od, FieldDictionary fd) { String jpql = "select t from EvaluateItemTemplate t where t.evaluateTypeEnum = :evaluateTypeEnum "; MapSqlParameterSource params = new MapSqlParameterSource(); params.addValue("evaluateTypeEnum", type); List<EvaluateItemTemplate> result = this.query(jpql, params); if (result.isEmpty()) { throw new RuntimeException("" + type.getName() + " ??"); } else if (result.size() == 1) { return result.get(0); } else { jpql += "and t.objectDictionary.id = :odID "; params.addValue("odID", od.getId()); result = this.query(jpql, params); if (result.size() == 1) { return result.get(0); } else { jpql += "and t.fieldDictionary.id = :fdID "; params.addValue("fdID", fd.getId()); result = this.query(jpql, params); if (result.size() == 1) { return result.get(0); } else if (result.isEmpty()) { throw new RuntimeException("" + type.getName() + " ?" + od.getDisplayname() + "?" + fd.getDisplayname() + "??"); } else { throw new RuntimeException( "" + type.getName() + " ?" + od.getDisplayname() + "?" + fd.getDisplayname() + "???"); } } } } }