1 package org.kuali.student.enrollment.class2.courseoffering.service.impl;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.kuali.rice.core.api.util.RiceKeyConstants;
5 import org.kuali.rice.krad.lookup.Lookupable;
6 import org.kuali.rice.krad.lookup.LookupableImpl;
7 import org.kuali.rice.krad.util.GlobalVariables;
8 import org.kuali.rice.krad.util.KRADConstants;
9 import org.kuali.rice.krad.web.form.LookupForm;
10 import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
11 import org.kuali.student.common.util.ContextBuilder;
12 import org.kuali.student.r2.common.exceptions.DoesNotExistException;
13 import org.kuali.student.r2.core.room.dto.BuildingInfo;
14 import org.kuali.student.r2.core.room.dto.RoomInfo;
15 import org.kuali.student.r2.core.room.service.RoomService;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map;
20
21
22
23
24 public class RoomInfoLookupableImpl extends LookupableImpl implements Lookupable {
25
26 private RoomService roomService;
27
28 @Override
29 public boolean validateSearchParameters(LookupForm form, Map<String, String> searchCriteria){
30 if (searchCriteria == null || searchCriteria.isEmpty() || StringUtils.isBlank(searchCriteria.get("buildingCode"))){
31 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Please enter the building code first to select a room");
32 return false;
33 }
34 return true;
35 }
36
37 @Override
38 protected List<?> getSearchResults(LookupForm lookupForm, Map<String, String> fieldValues, boolean unbounded) {
39 boolean validate = validateSearchParameters(lookupForm,fieldValues);
40 int firstBuilding = 0;
41 if (validate){
42 try {
43
44 List<BuildingInfo> buildings = getRoomService().getBuildingsByBuildingCode(fieldValues.get("buildingCode"), ContextBuilder.loadContextInfo());
45
46 if (buildings.isEmpty()){
47 GlobalVariables.getMessageMap().putInfo(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM,"Invalid building code");
48 return new ArrayList<RoomInfo>();
49 }
50
51 if (StringUtils.isBlank(fieldValues.get("roomCode"))){
52 List<String> roomIds = getRoomService().getRoomIdsByBuilding(buildings.get(firstBuilding).getId(), ContextBuilder.loadContextInfo());
53
54 if(roomIds.isEmpty()) {
55 return new ArrayList<RoomInfo>();
56 }
57
58 return getRoomService().getRoomsByIds(roomIds,ContextBuilder.loadContextInfo());
59 } else {
60 return getRoomService().getRoomsByBuildingAndRoomCode(buildings.get(firstBuilding).getId(),fieldValues.get("roomCode"),ContextBuilder.loadContextInfo());
61 }
62
63 } catch (DoesNotExistException e) {
64 return new ArrayList<RoomInfo>();
65 }catch (Exception e) {
66 throw new RuntimeException(e);
67 }
68 }
69
70 return new ArrayList<RoomInfo>();
71
72 }
73
74 public RoomService getRoomService(){
75 if (roomService == null){
76 roomService = CourseOfferingResourceLoader.loadRoomService();
77 }
78 return roomService;
79 }
80 }