I have written this code according to my requirements. But after finishing the code, it looks like there are too many loops. I have reduced it as much as I can. Can you find any improvements that I can make to this method?
The relationship is that one theater activity has many bookable resources. Note: Bookable resources are common to all activities, but the count of those bookable resources are unique to each activity.
private TheatrePlanResourceData[] buildTheatrePlanResourceData(Map<Long, List<TheatreResourceData>> theatreActivities)
throws SpiderException{
TheatrePlanResourceData[] result = new TheatrePlanResourceData[theatreActivities.size()];
Map<Long, Map<Long, Integer>> reverseBookableResMap = new HashMap<Long, Map<Long, Integer>>();
Map<Long, Integer> bookableResourceKeyToCountMapOutter = new LinkedHashMap<Long, Integer>();
int count = 0;
for (Map.Entry<Long, List<TheatreResourceData>> activityEntry : theatreActivities.entrySet())
{
List<TheatreResourceData> assistants = new ArrayList<TheatreResourceData>();
List<TheatreResourceData> surgeons = new ArrayList<TheatreResourceData>();
Map<Long, Integer> bookableResourceKeyToCountMap = new LinkedHashMap<Long, Integer>();
TheatrePlanResourceData planResourceData = new TheatrePlanResourceData();
planResourceData.theatreActivityKey = activityEntry.getKey();
List<TheatreResourceData> resourceData = (activityEntry.getValue() != null) ? activityEntry.getValue() : new ArrayList<TheatreResourceData>();
if (resourceData.size() > 0){
planResourceData.stateKey = ((TheatreResourceData) resourceData.get(0)).stateKey;
}
for (TheatreResourceData resData : resourceData){
switch (resData.type){
case TheatreResourceType.SURGEON1:
planResourceData.staff.surgeon1 = resData;
break;
case TheatreResourceType.ASSISTANT:
assistants.add(resData);
break;
case TheatreResourceType.OTHER_SURGEONS:
surgeons.add(resData);
break;
case TheatreResourceType.BOOKABLE_RESOURCE:
bookableResourceKeyToCountMap.put(Long.parseLong(resData.resourceID), resData.count);
bookableResourceKeyToCountMapOutter.put(Long.parseLong(resData.resourceID), resData.count);
break;
}
}
reverseBookableResMap.put(activityEntry.getKey(), bookableResourceKeyToCountMap);
planResourceData.staff.assistants = assistants.toArray(new TheatreResourceData[assistants.size()]);
planResourceData.staff.otherSurgeons = surgeons.toArray(new TheatreResourceData[surgeons.size()]);
result[count++] = planResourceData;
}
Long[] bookableResourceKeyList = bookableResourceKeyToCountMapOutter.keySet()
.toArray(new Long[bookableResourceKeyToCountMapOutter.keySet().size()]);
TheatreBookableResourceTypeData[] allBookableResourceTypeList = InternalTheatreBookableResourceTypeToolkit
.getInstance().getByKeys(CollectionUtils.toPrimitive(bookableResourceKeyList));
for (TheatrePlanResourceData resourceDataObj : result){
Map<Long, Integer> bookableResourceCountMap = reverseBookableResMap.get(resourceDataObj.theatreActivityKey);
TheatreBookableResourceTypeData[] relevantResourceList = new TheatreBookableResourceTypeData[bookableResourceCountMap
.size()];
int index = 0;
for (Map.Entry<Long, Integer> countEntry : bookableResourceCountMap.entrySet()){
for (TheatreBookableResourceTypeData resTypeObj : allBookableResourceTypeList){
if (countEntry.getKey() == resTypeObj.key){
try{
TheatreBookableResourceTypeData clonedResourceType = (TheatreBookableResourceTypeData) resTypeObj.clone();
clonedResourceType.count = countEntry.getValue();
relevantResourceList[index++] = clonedResourceType;
}catch (CloneNotSupportedException e){
TheatreModuleException.convert(e);
continue;
}
}
}
}
resourceDataObj.bookableRes = relevantResourceList;
}
return result;
}