package com.farata.example.data; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import com.farata.example.dto.AssociateDTO; import com.farata.example.dto.CompanyDTO; public class DataEngine { private DataEngine() { initializeCompanies(); initializeCompanyAssociates(); } private static DataEngine _instance; public static DataEngine getInstance(){ if (_instance==null) { _instance = new DataEngine(); } return _instance; } /* * companyList is rudimentary entity: only single thread is supposed to access it, * it serves only for illustration of the ChangeObject mechanics */ private ArrayList<CompanyDTO> companyList; @SuppressWarnings("serial") private void initializeCompanies() { companyList = new ArrayList<CompanyDTO>() { public boolean add(CompanyDTO dto) { if ((dto==null) || (dto.getId()==null)) return false; ArrayList<AssociateDTO> associateList = new ArrayList<AssociateDTO>(); if (companyAssociateMap == null) { companyAssociateMap = new HashMap<Integer, ArrayList<AssociateDTO>>(); } companyAssociateMap.put(dto.getId(), associateList); return super.add(dto); } public CompanyDTO remove(int index) { CompanyDTO dto = super.remove(index); if (dto.getId()!=null) { companyAssociateMap.remove(dto.getId()); } return dto; } }; CompanyDTO dto = new CompanyDTO(); dto.setCompanyName("FarataSystems"); dto.setId(1); companyList.add(dto); dto = new CompanyDTO(); dto.setCompanyName("Sencha"); dto.setId(2); companyList.add(dto); } public List<CompanyDTO> getCompanyList() { List<CompanyDTO> list = new ArrayList<CompanyDTO>(companyList); return list; } public void addCompany(CompanyDTO company) { companyList.add(company); } public CompanyDTO findCompany(Integer id) { CompanyDTO companyDTO = null; for (int i=0; i< companyList.size(); i++) { CompanyDTO listDTO = companyList.get(i) ; if (listDTO.getId().compareTo(id)==0) { companyDTO = listDTO; break; } } return companyDTO; } public CompanyDTO findCompany(CompanyDTO dto) { return findCompany(dto.getId()); } public void updateCompany(CompanyDTO previousVersion, CompanyDTO newVersion, String[] changedPropertyNames) { CompanyDTO originalDTO = findCompany(newVersion.getId()) ; String originalValue,newValue; for (String propertyName: changedPropertyNames) { if ("companyName".equals(propertyName)) { originalValue = originalDTO.getCompanyName(); newValue = newVersion.getCompanyName(); originalDTO.setCompanyName(newVersion.getCompanyName()); } else { originalValue = originalDTO.getId().toString(); newValue = newVersion.getId().toString(); originalDTO.setId(newVersion.getId()); } System.out.println("Changed: " + propertyName + ", Original Value: " + originalValue + ", New Value: " + newValue); } } public CompanyDTO removeCompany(Integer id) { CompanyDTO removed = null; for (int i=0; i< companyList.size(); i++) { CompanyDTO listDTO = companyList.get(i) ; if (listDTO.getId().compareTo(id)==0) { removed = companyList.remove(i); break; } } return removed; } public CompanyDTO removeCompany(CompanyDTO dto) { return removeCompany(dto.getId()); } public Integer getMaxCompanyId() { int maxId=0; try { CompanyDTO max = (CompanyDTO)Collections.max(companyList, new CompanyIdComparator()); maxId = max.getId(); } catch (NoSuchElementException nsee) { // default to 0 } return new Integer(maxId); } class CompanyIdComparator implements Comparator<CompanyDTO> { @Override public int compare(CompanyDTO o1, CompanyDTO o2) { return o1.getId().compareTo(o2.getId()); } } /* * companyAssociateMap is rudimentary support of one to many company-associate * relationship, only single thread is supposed to access it, * it serves only for illustration of the ChangeObject mechanics */ private Map<Integer, ArrayList<AssociateDTO>> companyAssociateMap; private void initializeCompanyAssociates() { //Farata Associates Integer companyId = new Integer(1); ArrayList<AssociateDTO> associateList = companyAssociateMap.get(companyId); AssociateDTO dto = new AssociateDTO(); dto.setAssociateName("Yakov Fain"); dto.setCompanyId(companyId); dto.setId(1); associateList.add(dto); dto = new AssociateDTO(); dto.setAssociateName("Anatole Tartakovsky"); dto.setCompanyId(companyId); dto.setId(2); associateList.add(dto); dto = new AssociateDTO(); dto.setAssociateName("Victor Rasputnis"); dto.setCompanyId(companyId); dto.setId(3); associateList.add(dto); //Sencha Team Associates companyId = new Integer(2); associateList = companyAssociateMap.get(companyId); dto = new AssociateDTO(); dto.setAssociateName("Ed Spenser"); dto.setCompanyId(companyId); dto.setId(4); associateList.add(dto); dto = new AssociateDTO(); dto.setAssociateName("Ted Patrick"); dto.setCompanyId(companyId); dto.setId(5); associateList.add(dto); } public List<AssociateDTO> getAssociateList(Integer companyId) { if (companyId==null) throw new RuntimeException("companyId can not be null"); ArrayList<AssociateDTO> associateList = companyAssociateMap.get(companyId); return associateList; } public AssociateDTO findAssociate(AssociateDTO dto) { AssociateDTO targetDTO=null; Integer companyId = dto.getCompanyId(); if (companyId == null) throw new RuntimeException("companyId can not be null"); List<AssociateDTO> associateList = companyAssociateMap.get(companyId); if (associateList != null) { for (AssociateDTO listDTO: associateList) { if (listDTO.getId().compareTo(dto.getId())==0) { targetDTO = listDTO; break; } } } return targetDTO; } public void addAssociate(AssociateDTO dto) { Integer companyId = dto.getCompanyId(); List<AssociateDTO> associateList = getAssociateList(companyId); associateList.add(dto); } public void updateAssociate(AssociateDTO previousVersion, AssociateDTO newVersion, String[] changedPropertyNames) { AssociateDTO originalDTO = findAssociate(previousVersion); String originalValue, newValue; for (String propertyName: changedPropertyNames) { if ("associateName".equals(propertyName)) { originalValue = originalDTO.getAssociateName(); newValue = newVersion.getAssociateName(); originalDTO.setAssociateName(newVersion.getAssociateName()); } else { originalValue = originalDTO.getId().toString(); newValue = newVersion.getId().toString(); originalDTO.setId(newVersion.getId()); } System.out.println("Changed: " + propertyName + ", Original Value: " + originalValue + ", New Value: " + newValue); } } public AssociateDTO removeAssociate(AssociateDTO dto) { AssociateDTO removedDTO=null; Integer companyId = dto.getCompanyId(); if (companyId == null) throw new RuntimeException("companyId can not be null"); List<AssociateDTO> associateList = companyAssociateMap.get(companyId); if (associateList!=null) { for (AssociateDTO listDTO: associateList) { if (listDTO.getId().compareTo(dto.getId())==0) { associateList.remove(listDTO); removedDTO = listDTO; break; } } } return removedDTO; } @SuppressWarnings("unchecked") public Integer getMaxCompanyAssociateId () { int maxId=0; Iterator it = companyAssociateMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry)it.next(); ArrayList<AssociateDTO> partialList = (ArrayList<AssociateDTO>) pairs.getValue(); try { AssociateDTO maxDTO = (AssociateDTO)Collections.max(partialList, new AssociateIdComparator()); if (maxDTO.getId() > maxId) maxId = maxDTO.getId(); } catch (NoSuchElementException nsee) { // default to 0 } } return new Integer(maxId); } class AssociateIdComparator implements Comparator<AssociateDTO> { @Override public int compare(AssociateDTO o1, AssociateDTO o2) { return o1.getId().compareTo(o2.getId()); } } }