Author: DavidK Date: 2010-02-24 15:03:14 +0100 (Wed, 24 Feb 2010) New Revision: 1847 Modified: trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBL.java trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLImpl.java trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLTest.java trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImpl.java Log: BL updated- category, company, elvys Modified: trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBL.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBL.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBL.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -324,10 +324,7 @@ */ public List<CategoryDefinition> listCatDefinitionsForLayout(Integer layoutId, Session sess) throws ExecuteException; - - - /** * returns all category definition object which are used by given group * Fetches Layout and Layout.Company. @@ -344,6 +341,24 @@ /** + * Returns all catDefs of company that aren't assigned to specified group.<br/> + * Fetches: Layout and Layout.Company.<br/> + * Orders by 'created' (newer first) + * @param groupId id of the group + * @return list of category definition + * @throws ExecuteException + */ + public List<CategoryDefinition> listCatDefinitionsUnusedForGroupMakeSession(Integer groupId) throws ExecuteException; + + /** + * @see CategoryBL#listCatDefinitionsUnusedForGroupMakeSession(Integer) + */ + public List<CategoryDefinition> listCatDefinitionsUnusedForGroup(Integer groupId, Session sess) throws ExecuteException; + + + + + /** * returns all category definition object which given elvys uses * Fetches Layout and Layout.Company. * Orders by 'created' (newer first) Modified: trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLImpl.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLImpl.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLImpl.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -15,6 +15,7 @@ import cz.elvys.webServer.HIBgen.Category; import cz.elvys.webServer.HIBgen.CategoryDefinition; import cz.elvys.webServer.HIBgen.CategoryPlanning; +import cz.elvys.webServer.HIBgen.Company; import cz.elvys.webServer.HIBgen.ElvysGroup; import cz.elvys.webServer.HIBgen.Layout; import cz.elvys.webServer.HIBgen.Panel; @@ -25,6 +26,7 @@ import elvys.server.bl.Fact; import elvys.server.bl.ValidationResult; import elvys.server.bl.ValidationResult.VRRecord; +import elvys.server.bl.companyRel.CompanyBL; import elvys.server.bl.elvysrel.ElvysBL; import elvys.server.bl.layoutrel.LayoutBL; import elvys.server.bl.planning.PlanningBL; @@ -61,12 +63,14 @@ private ElvysBL ebl = null; private LayoutBL lbl = null; private PlanningBL pbl = null; + private CompanyBL companyBL = null; { ebl = Fact.getElvysBL(); lbl = Fact.getLayoutBL(); pbl = Fact.getPlanningBL(); + companyBL = Fact.getCompanyBL(); } @@ -725,6 +729,47 @@ } + @Override + public List<CategoryDefinition> listCatDefinitionsUnusedForGroupMakeSession( + Integer groupId) throws ExecuteException { + Session sess = InitSessionFactory.getInstance().getCurrentSession(); + Transaction tx = HibUtils.startTx(sess); + List<CategoryDefinition> list = null; + try { + list = listCatDefinitionsUnusedForGroup(groupId, sess); + tx.commit(); + } catch (Exception e) { + tx.rollback(); + throw new ExecuteException(e); + } + return list; + } + + @Override + public List<CategoryDefinition> listCatDefinitionsUnusedForGroup( + Integer groupId, Session sess) throws ExecuteException { + List<CategoryDefinition> defs = null; + try { + Company company = companyBL.getCompanyByGroupId(groupId, sess); + Integer companyId = company.getId(); + + Query q = sess.createQuery("from CategoryDefinition cd " + + "inner join fetch cd.layout " + + "inner join fetch cd.layout.company comp " + + "where comp.id = :companyId " + + " and cd.disabled = false " + + " and :groupId not in (select cat.elvysGroup.id from Category cat where cat.categoryDefinition.id = cd.id)"); + q.setInteger("groupId", groupId); + q.setInteger("companyId", companyId); + defs = q.list(); + } catch (Exception e) { + String message = CommonUtils.prepareErrorMessage(CANNOT_LIST_CAT_DEF, + "groupId", groupId); + throw new ExecuteException(message,e); + } + return defs; + } + @Override public List<CategoryDefinition> listCatDefinitionsOfCompanyMakeSession(Integer companyId) @@ -742,6 +787,9 @@ return list; } + + + @Override public List<CategoryDefinition> listCatDefinitionsOfCompany(Integer companyId, Session sess) throws ExecuteException { Modified: trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLTest.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLTest.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/categrel/CategoryBLTest.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -12,6 +12,7 @@ import static org.junit.Assert.*; +import cz.elvys.commons.utils.CommonUtils; import cz.elvys.webServer.HIBgen.Category; import cz.elvys.webServer.HIBgen.CategoryDefinition; import cz.elvys.webServer.HIBgen.CategoryPlanning; @@ -81,6 +82,7 @@ throw new RuntimeException("not implemented yet"); } + @Ignore @Test public void WRITING_testReplicateCategoryPlanning() throws Exception { int replicated=0; @@ -302,10 +304,65 @@ @Test public void testListCatDefinitionsOfGroup() throws Exception { - throw new RuntimeException("not implemented yet"); + int cgroups = 0; + int ccatdefs = 0; + + // groups + List<ElvysGroup> groups = ebl.listGroupByCompany(companyId); + for(ElvysGroup group: groups) { + + // catdefs of group + List<CategoryDefinition> catDefs = cbl.listCatDefinitionsOfGroupMakeSession(group.getId()); + assertCatDefinitionsEmptyable(catDefs); + ccatdefs += catDefs.size(); + } + cgroups = groups.size(); + + assertTrue(cgroups>0); + assertTrue(ccatdefs>0); } @Test + public void testListCatDefinitionsUnusedForGroup() throws Exception { + int cgroups = 0; + int cCDs = 0; + int cCDused = 0; + int cCDunused = 0; + + // catdefs of company + List<CategoryDefinition> compCatDefs = cbl.listCatDefinitionsOfCompanyMakeSession(companyId); + cCDs = compCatDefs.size(); + + // groups + List<ElvysGroup> groups = ebl.listGroupByCompany(companyId); + for(ElvysGroup group: groups) { + + // catdefs used on group + List<CategoryDefinition> usedCD = cbl.listCatDefinitionsOfGroupMakeSession(group.getId()); + assertCatDefinitionsEmptyable(usedCD); + cCDused += usedCD.size(); + + + // catdefs not-used on group + List<CategoryDefinition> notusedCD = cbl.listCatDefinitionsUnusedForGroupMakeSession(group.getId()); + assertCatDefinitionsEmptyable(notusedCD); + cCDunused += notusedCD.size(); + + System.out.println(CommonUtils.prepareErrorMessage("", "groupId", group.getId(), + "usedCD",usedCD.size(),"unusedCD",notusedCD.size(),"companyCD",compCatDefs.size())); + assertTrue(usedCD.size()+notusedCD.size() == compCatDefs.size()); + } + cgroups = groups.size(); + + assertTrue(cgroups>0); + assertTrue(cCDs>0); + assertTrue(cCDused>0); + assertTrue(cCDunused>0); + + } + + + @Test public void testlistCatDefinitionsOfCompany() throws Exception { // get all category definitions of the company List<CategoryDefinition> list = cbl.listCatDefinitionsOfCompanyMakeSession(companyId); Modified: trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBL.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -5,7 +5,6 @@ import org.hibernate.Session; import cz.elvys.webServer.HIBgen.Company; -import cz.elvys.webServer.HIBgen.User; import cz.elvys.webServer.toSpring.exception.ExecuteException; @@ -30,14 +29,29 @@ List<Company> getListOfCompanies(CompanyType type) throws Exception; /** - * Return object of company by company id. + * Returns object of company by company id. * @param companyId * @return */ - public Company getCompanyByIdMakeSession(Integer companyId); - public Company getCompanyById(Integer companyId,Session sess); + public Company getCompanyByIdMakeSession(Integer companyId) throws ExecuteException; + public Company getCompanyById(Integer companyId,Session sess) throws ExecuteException; + /** + * Returns company by id of group. + * @param groupId id of the group + * @return null + * @throws Exception + */ + public Company getCompanyByGroupIdMakeSession(Integer groupId) throws ExecuteException; + + /** + * @see CompanyBL#getCompanyByGroupIdMakeSession(Integer, Session) + */ + public Company getCompanyByGroupId(Integer groupId, Session sess) throws ExecuteException; + + + /** * Method for loading user by login. There is created session. * @param login Username of user * @return User Object of User @@ -57,5 +71,4 @@ * @param company */ public void deleteCompany(Company company); - } Modified: trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImpl.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -5,20 +5,15 @@ import java.util.List; import org.apache.log4j.Logger; -import org.bouncycastle.asn1.isismtt.x509.Restriction; -import org.hibernate.Criteria; -import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; -import org.hibernate.criterion.Restrictions; import cz.elvys.commons.def.PropertyManager; import cz.elvys.commons.utils.CommonUtils; import cz.elvys.commons.utils.HibUtils; import cz.elvys.webServer.HIBgen.CompSetting; import cz.elvys.webServer.HIBgen.Company; -import cz.elvys.webServer.HIBgen.User; import cz.elvys.webServer.db.InitSessionFactory; import cz.elvys.webServer.toSpring.exception.ExecuteException; @@ -28,13 +23,14 @@ // messages private static final String CANNOT_LOAD_COMPANIES = "Couldn't load list of companies."; + private static final String CANNOT_LOAD_COMPANY = "Couldn't load company from DB"; static { log = Logger.getLogger(CompanyBLImpl.class); } @Override - public List<Company> getListOfCompanies(CompanyType type) throws Exception { + public List<Company> getListOfCompanies(CompanyType type) throws ExecuteException { Session sess = InitSessionFactory.getInstance().getCurrentSession(); Transaction tx = HibUtils.startTx(sess); @@ -59,42 +55,85 @@ String message = CommonUtils.prepareErrorMessage( CANNOT_LOAD_COMPANIES, "whichCompanies", type); log.error(message,e); - throw new Exception(message,e); + throw new ExecuteException(message,e); } return companies; } + @Override + public Company getCompanyByIdMakeSession(Integer companyId) throws ExecuteException { + Session sess = InitSessionFactory.getInstance().getCurrentSession(); + Transaction tx = HibUtils.startTx(sess); + Company company = null; + try { + company = getCompanyById(companyId, sess); + tx.commit(); + } catch (Exception e) { + tx.rollback(); + throw new ExecuteException("User couldn't be obtained from DB",e); + } + return company; + } + @Override - public Company getCompanyByIdMakeSession(Integer companyId) { - Session session = InitSessionFactory.getInstance().getCurrentSession(); - Transaction tx = session.getTransaction(); + public Company getCompanyById(Integer companyId,Session sess) throws ExecuteException { Company company = null; try { - tx.begin(); + + Query query = sess.createQuery("from Company company " + + "where company.id=:compId"); + query.setInteger("compId", companyId); + List<Company> companies = query.list(); + if(companies.size() == 1){ + company = companies.get(0); + } else { + company = null; + } } catch (Exception e) { - throw new Error("Company couldn't be obtained from DB",e); + String message = CommonUtils.prepareErrorMessage("couldn't get company", "companyId", companyId); + throw new RuntimeException(message,e); } - + return company; + } + + + @Override + public Company getCompanyByGroupIdMakeSession(Integer groupId) throws ExecuteException { + Session sess = InitSessionFactory.getInstance().getCurrentSession(); + Transaction tx = HibUtils.startTx(sess); + Company company = null; try { - company = getCompanyById(companyId, session); + company = getCompanyByGroupId(groupId, sess); tx.commit(); } catch (Exception e) { tx.rollback(); - throw new Error("User couldn't be obtained from DB",e); + throw new ExecuteException(e); } return company; - } + } @Override - public Company getCompanyById(Integer companyId,Session sess) { - Query query = sess.createQuery("from Company company " + - "where company.id=:compId"); - query.setInteger("compId", companyId); - List<Company> companies = query.list(); - if(companies.size() == 1){ - return companies.get(0); - } - return null; + public Company getCompanyByGroupId(Integer groupId, Session sess) throws ExecuteException { + Company company = null; + try { + Query q = sess.createQuery("SELECT comp FROM Company comp, " + + "Elvys elv, " + + "ElvysGroup g " + + "WHERE g.id=:groupId " + + " and elv.elvysGroup=g.id " + + " and elv.company=comp.id "); + q.setInteger("groupId", groupId); + if (q.list().size() == 0) { + company = null; + } else { + company = (Company)q.list().get(0); + } + } catch (Exception e) { + String message = CommonUtils.prepareErrorMessage("couldn't load company", + "groupId", groupId); + throw new ExecuteException(message,e); + } + return company; } Modified: trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/companyRel/CompanyBLImplTest.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -1,40 +1,94 @@ package elvys.server.bl.companyRel; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.util.List; +import org.junit.BeforeClass; import org.junit.Test; import cz.elvys.webServer.HIBgen.Company; +import cz.elvys.webServer.HIBgen.ElvysGroup; +import elvys.server.bl.Fact; import elvys.server.bl.companyRel.CompanyBL.CompanyType; +import elvys.server.bl.elvysrel.ElvysBL; public class CompanyBLImplTest { + // const + private static final Integer compId = 4; + + // other fields + private static CompanyBL cbl = null; + private static ElvysBL ebl = null; + + + + @BeforeClass + public static void initME() { + cbl = Fact.getCompanyBL(); + ebl = Fact.getElvysBL(); + } + + @Test public void getListOfNonSystemCompanies() throws Exception { - CompanyBLImpl impl = new CompanyBLImpl(); - List<Company> companies = impl.getListOfCompanies(CompanyType.NOT_SYSTEM_ONLY); + List<Company> companies = cbl.getListOfCompanies(CompanyType.NOT_SYSTEM_ONLY); printCompanies(companies); } @Test public void getListOfSystemCompanies() throws Exception { - CompanyBLImpl impl = new CompanyBLImpl(); - List<Company> companies = impl.getListOfCompanies(CompanyType.SYSTEM_ONLY); + List<Company> companies = cbl.getListOfCompanies(CompanyType.SYSTEM_ONLY); printCompanies(companies); } @Test public void getListOfAllCompanies() throws Exception { - CompanyBLImpl impl = new CompanyBLImpl(); - List<Company> companies = impl.getListOfCompanies(CompanyType.ALL); + List<Company> companies = cbl.getListOfCompanies(CompanyType.ALL); printCompanies(companies); } + @Test + public void testGetCompanyByGroupId() throws Exception { + // found companies + int found = 0; + + // list of groups + List<ElvysGroup> groups = ebl.listGroupByCompany(compId); + for (ElvysGroup group: groups) { + Company company = cbl.getCompanyByGroupIdMakeSession(group.getId()); + assertCompany(company); + found++; + } + assertTrue(found > 0); + } + private void printCompanies(List<Company> companies) { for (Company co : companies) { System.out.print(co.getName()+"; "); } System.out.println(); } + + + + private void assertCompanies(List<Company> companies) { + assertTrue(companies != null && companies.size() > 0); + for (Company comp: companies) { + assertCompany(comp); + } + } + private void assertCompany(Company comp) { + assertTrue(comp!= null); + assertFalse(comp.isDisabled()); + assertTrue(comp.getId() != null); + assertTrue(comp.getName() != null); + assertTrue(comp.getCity() != null); + } + + + } Modified: trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImpl.java =================================================================== --- trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImpl.java 2010-02-24 13:33:04 UTC (rev 1846) +++ trunk/server/elvysCommons/src/elvys/server/bl/elvysrel/ElvysBLImpl.java 2010-02-24 14:03:14 UTC (rev 1847) @@ -302,7 +302,6 @@ @Override public void saveElvys(Elvys elvys,Integer resolutionId,Integer companyId) { - if(resolutionId == null){ throw new NullPointerException("Resolution cannot be null."); } @@ -323,13 +322,6 @@ } elvys.setResolution(resolution); - CompanyBL cbl = Fact.getCompanyBL(); - Company company = cbl.getCompanyByIdMakeSession(companyId); - if(company == null){ - throw new NullPointerException("Company with specified id cannot exists"); - } - elvys.setCompany(company); - ElvysSetting es = null; ElvysGroup eg = null; @@ -349,13 +341,15 @@ Session sess = InitSessionFactory.getInstance().getCurrentSession(); - Transaction tx = sess.getTransaction(); + Transaction tx = HibUtils.startTx(sess); try { - tx.begin(); - } catch(HibernateException e) { - throw new Error("error occured when initializing hibernate session",e); - } - try { + CompanyBL cbl = Fact.getCompanyBL(); + Company company = cbl.getCompanyById(companyId,sess); + if(company == null){ + throw new NullPointerException("Company with specified id doesn't exists"); + } + elvys.setCompany(company); + if(elvys.getId()==0){ sess.saveOrUpdate(es); sess.saveOrUpdate(eg);