@GET @Path("CleanOccupations") public String cleanOccupations() { String sqlRD = "SELECT rd FROM ResourceDescriptor rd"; TypedQuery<ResourceDescriptor> allRd = this.getEntityManager().createQuery(sqlRD, ResourceDescriptor.class); StringBuilder output = new StringBuilder(); for (ResourceDescriptor rd : allRd.getResultList()) { ResourceInstance defaultInstance = rd.getDefaultInstance(); List<Occupation> occupations = defaultInstance.getOccupations(); HashMap<Long, List<Occupation>> map = new HashMap<>(); List<Occupation> cleanList = new ArrayList<>(); for (Occupation occ : occupations) { Long key = ((Double) occ.getTime()).longValue(); if (!map.containsKey(key)) { map.put(key, new ArrayList<>()); cleanList.add(occ); } map.get(key).add(occ); } Long created = 0L; /** make sure each occupation exists for each resources */ Collection<ResourceInstance> resourceInstances = rd.getScope().getPrivateInstances().values(); for (ResourceInstance resourceInstance : resourceInstances) { for (Entry<Long, List<Occupation>> entry : map.entrySet()) { if (!hasOccupation(resourceInstance, entry.getKey().doubleValue())) { resourceFacade.addOccupation(resourceInstance.getId(), false, entry.getKey()); created++; } } } if (cleanList.size() != occupations.size()) { output .append("CLEAN OCCUPATIONS FOR ") .append(rd.getLabel()) .append(" (from ") .append(occupations.size()) .append(" to ") .append(cleanList.size()) .append("; ") .append(created) .append(" propagated)") .append("<br />"); defaultInstance.setOccupations(cleanList); } } return output.toString(); }
private boolean hasOccupation(ResourceInstance ri, double time) { for (Occupation o : ri.getOccupations()) { if (Math.abs(o.getTime() - time) < 0.000001) { return true; } } return false; }