System.out.println("Поиск полисов - контракт " + getExistentEntity().getId());
List<Polis> polisList = findEntityList(Polis.class, "contract.id", getExistentEntity().getId());
System.out.println("Маппирование групп - количество полисов " + polisList.size());
HashMap<PolisUniq, ArrayList<Polis>> polisMap = new HashMap<PolisUniq, ArrayList<Polis>>();
for (int i = 0; i < polisList.size(); i++) {
Polis polis = polisList.get(i);
PolisUniq pu = new PolisUniq(polis);
ArrayList<Polis> array = polisMap.get(pu);
if (array == null) {
array = new ArrayList<Polis>();
polisMap.put(pu, array);
}
array.add(polis);
}
ArrayList<ArrayList<Integer>> groupList = new ArrayList<ArrayList<Integer>>();
Set<PolisUniq> keySet = polisMap.keySet();
for (Iterator<PolisUniq> it = keySet.iterator(); it.hasNext();) {
PolisUniq polisUniq = it.next();
ArrayList<Polis> pList = polisMap.get(polisUniq);
if (pList.size() < 2) {
it.remove();
} else {
ArrayList<Integer> array = new ArrayList<Integer>();
for (Polis polis : pList) {
array.add(polis.getId());
}
groupList.add(array);
}
}
System.out.println("Объединение полисов - количество групп " + polisMap.size());
polisMap = null;
keySet = null;
polisList = null;
System.gc();
int counter = 0;
float size = groupList.size();
for (ArrayList<Integer> array : groupList) {
counter ++;
System.out.println("Выполнено " + counter/size*100 + "%");
if (array.size() > 1) {
//Объединение полисов
Integer mainID = null;
for (Integer polisID : array) {
if (mainID == null || polisID > mainID) {
mainID = polisID;
}
}
Polis main = findEntity(Polis.class, mainID);
boolean inTrash = main.isTrash();
for (Integer polisID : array) {
if (polisID != mainID) {
//Перевязка ссылок
Polis polis = findEntity(Polis.class, polisID);
if (!polis.isTrash()) {
inTrash = false;
}
List<ServiceRender> serrenList = findEntityList(ServiceRender.class, "polis.id", polisID);
for (int i = 0; i < serrenList.size(); i++) {
ServiceRender serviceRender = serrenList.get(i);
serviceRender.setPolis(main);
saveEntity(serviceRender);
}
List<ProfcheckupItem> profCheckupList = findEntityList(ProfcheckupItem.class, "polis.id", polisID);
for (int i = 0; i < profCheckupList.size(); i++) {
ProfcheckupItem profcheckupItem = profCheckupList.get(i);
profcheckupItem.setPolis(main);
saveEntity(profcheckupItem);
}
Address address = polis.getAddress();
if (address != null) {
removeEntity(address);
}
removeEntity(polis);
}