protected RoomFeature lookupFeature( org.hibernate.Session hibSession, FeatureInterface original, boolean future, Long sessionId) { if (original == null) return null; if (future) { if (original.isDepartmental()) return (DepartmentRoomFeature) hibSession .createQuery( "select f from DepartmentRoomFeature f, DepartmentRoomFeature o where o.uniqueId = :originalId and f.department.session.uniqueId = :sessionId " + "and f.abbv = o.abbv and f.department.deptCode = o.department.deptCode") .setLong("sessionId", sessionId) .setLong("originalId", original.getId()) .setCacheable(true) .setMaxResults(1) .uniqueResult(); else return (GlobalRoomFeature) hibSession .createQuery( "select f from GlobalRoomFeature f, GlobalRoomFeature o where o.uniqueId = :originalId and f.session.uniqueId = :sessionId " + "and f.abbv = o.abbv") .setLong("sessionId", sessionId) .setLong("originalId", original.getId()) .setCacheable(true) .setMaxResults(1) .uniqueResult(); } else { return RoomFeatureDAO.getInstance().get(original.getId(), hibSession); } }
@Override public FeatureInterface execute(UpdateRoomFeatureRequest request, SessionContext context) { if (request.hasSessionId()) context = new EventContext(context, request.getSessionId()); Transaction tx = null; RoomFeature f = null; try { org.hibernate.Session hibSession = new RoomDeptDAO().getSession(); tx = hibSession.beginTransaction(); if (request.hasFeature()) { if (request.hasFutureSessions()) for (Long id : request.getFutureSessions()) createOrUpdateFeature( request.getFeature(), request.getAddLocations(), request.getDropLocations(), id, hibSession, new EventContext(context, context.getUser(), id), true); f = createOrUpdateFeature( request.getFeature(), request.getAddLocations(), request.getDropLocations(), context.getUser().getCurrentAcademicSessionId(), hibSession, context, false); } else if (request.getDeleteFeatureId() != null) { if (request.hasFutureSessions()) for (Long id : request.getFutureSessions()) dropFeature( request.getDeleteFeatureId(), id, hibSession, new EventContext(context, context.getUser(), id), true); dropFeature( request.getDeleteFeatureId(), context.getUser().getCurrentAcademicSessionId(), hibSession, context, false); } else { throw new GwtRpcException("Bad request."); } FeatureInterface feature = null; if (f != null) { feature = new FeatureInterface(f.getUniqueId(), f.getAbbv(), f.getLabel()); if (f.getFeatureType() != null) feature.setType( new FeatureTypeInterface( f.getFeatureType().getUniqueId(), f.getFeatureType().getReference(), f.getFeatureType().getLabel(), f.getFeatureType().isShowInEventManagement())); if (f instanceof DepartmentRoomFeature) { Department d = ((DepartmentRoomFeature) f).getDepartment(); feature.setDepartment(RoomDetailsBackend.wrap(d, null, null)); feature.setTitle( f.getLabel() + " (" + d.getName() + (f.getFeatureType() == null ? "" : ", " + f.getFeatureType().getLabel()) + ")"); } else { feature.setTitle( f.getLabel() + (f.getFeatureType() == null ? "" : " (" + f.getFeatureType().getLabel() + ")")); } } tx.commit(); return feature; } catch (Exception e) { e.printStackTrace(); if (tx != null) tx.rollback(); if (e instanceof GwtRpcException) throw (GwtRpcException) e; throw new GwtRpcException(e.getMessage()); } }
protected RoomFeature createOrUpdateFeature( FeatureInterface feature, List<Long> add, List<Long> drop, Long sessionId, org.hibernate.Session hibSession, SessionContext context, boolean future) { Department d = feature.isDepartmental() ? lookuDepartment(hibSession, feature.getDepartment(), future, sessionId) : null; if (feature.isDepartmental() && d == null) return null; RoomFeature rf = (feature.getId() == null ? null : lookupFeature(hibSession, feature, future, sessionId)); if (rf == null) { if (!future && feature.getId() != null) throw new GwtRpcException(MESSAGES.errorRoomFeatureDoesNotExist(feature.getId())); if (d == null) { context.checkPermission(Right.GlobalRoomFeatureAdd); rf = new GlobalRoomFeature(); ((GlobalRoomFeature) rf).setSession(SessionDAO.getInstance().get(sessionId)); } else { context.checkPermission(d, Right.DepartmentRoomFeatureAdd); rf = new DepartmentRoomFeature(); ((DepartmentRoomFeature) rf).setDepartment(d); } rf.setRooms(new HashSet<Location>()); } else { if (rf instanceof GlobalRoomFeature) { context.checkPermission(rf, Right.GlobalRoomFeatureEdit); } else { context.checkPermission(rf, Right.DepartmenalRoomFeatureEdit); ((DepartmentRoomFeature) rf).setDepartment(d); } } for (Iterator i = RoomFeature.getAllGlobalRoomFeatures(sessionId).iterator(); i.hasNext(); ) { RoomFeature x = (RoomFeature) i.next(); if ((x.getLabel().equalsIgnoreCase(feature.getLabel()) || x.getAbbv().equalsIgnoreCase(feature.getAbbreviation())) && !x.getUniqueId().equals(rf.getUniqueId())) throw new GwtRpcException( MESSAGES.errorRoomFeatureAlreadyExists( feature.getLabel(), SessionDAO.getInstance().get(sessionId).getLabel())); } if (rf instanceof DepartmentRoomFeature) { for (Iterator i = RoomFeature.getAllDepartmentRoomFeatures(d).iterator(); i.hasNext(); ) { RoomFeature x = (RoomFeature) i.next(); if ((x.getLabel().equalsIgnoreCase(feature.getLabel()) || x.getAbbv().equalsIgnoreCase(feature.getAbbreviation())) && !x.getUniqueId().equals(rf.getUniqueId())) throw new GwtRpcException( MESSAGES.errorRoomFeatureAlreadyExists( feature.getLabel(), d.getSession().getLabel())); } } rf.setAbbv(feature.getAbbreviation()); rf.setLabel(feature.getLabel()); rf.setFeatureType( feature.getType() == null ? null : RoomFeatureTypeDAO.getInstance().get(feature.getType().getId(), hibSession)); hibSession.saveOrUpdate(rf); if (add != null && !add.isEmpty()) for (Location location : lookupLocations(hibSession, add, future, sessionId)) { rf.getRooms().add(location); location.getFeatures().add(rf); hibSession.saveOrUpdate(location); } if (drop != null && !drop.isEmpty()) for (Location location : lookupLocations(hibSession, drop, future, sessionId)) { rf.getRooms().remove(location); location.getFeatures().remove(rf); hibSession.saveOrUpdate(location); } hibSession.saveOrUpdate(rf); ChangeLog.addChange( hibSession, context, rf, ChangeLog.Source.ROOM_FEATURE_EDIT, (feature.getId() == null ? ChangeLog.Operation.CREATE : ChangeLog.Operation.UPDATE), null, rf instanceof DepartmentRoomFeature ? ((DepartmentRoomFeature) rf).getDepartment() : null); return rf; }