Skip to content

Commit

Permalink
API: Buildings
Browse files Browse the repository at this point in the history
- added buildings API that can return
  - return list of buildings for an academic session (GET api/buildings?term=<TERM> or GET api/buildings?sessionId=<TERM ID>)
  - creature or update a building (using POST request with the building as a payload)
  - delete a building (DELETE api/buildings?id=<BUILDING ID> or DELETE api/buildings?term=<TERM>&externalId=<EXTID> or DELETE api/buildings?term=<TERM>&building=<ABBV>)
- permissions
  - ApiRetrieveRooms to retrieve list of buildings (GET request)
  - ApiRoomEdit to create, update, and delete a room, together with the appropriate BuildingAdd, BuildingEdit and BuildingDelete permission (POST and DELETE requests)
  • Loading branch information
tomas-muller committed Jun 17, 2016
1 parent 65f15e4 commit 9b19bfa
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.unitime.timetable.api.connectors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;
import org.springframework.stereotype.Service;
import org.unitime.timetable.api.ApiConnector;
import org.unitime.timetable.api.ApiHelper;
import org.unitime.timetable.gwt.shared.RoomInterface.BuildingInterface;
import org.unitime.timetable.model.Assignment;
import org.unitime.timetable.model.Building;
import org.unitime.timetable.model.ChangeLog;
import org.unitime.timetable.model.Room;
import org.unitime.timetable.model.TimetableManager;
import org.unitime.timetable.model.dao.BuildingDAO;
import org.unitime.timetable.model.dao.SessionDAO;
import org.unitime.timetable.security.rights.Right;

/**
* @author Tomas Muller
*/
@Service("/api/buildings")
public class BuildingsConntector extends ApiConnector {
@Override
public void doGet(ApiHelper helper) throws IOException {
Long sessionId = helper.getAcademicSessionId();
if (sessionId == null)
throw new IllegalArgumentException("Academic session not provided, please set the term parameter.");

helper.getSessionContext().checkPermissionAnyAuthority(sessionId, "Session", Right.ApiRetrieveRooms);

List<BuildingInterface> buildings = new ArrayList<BuildingInterface>();
for (Building b: Building.findAll(sessionId)) {
BuildingInterface building = new BuildingInterface();
building.setId(b.getUniqueId());
building.setName(b.getName());
building.setAbbreviation(b.getAbbreviation());
building.setX(b.getCoordinateX());
building.setY(b.getCoordinateY());
building.setExternalId(b.getExternalUniqueId());
buildings.add(building);
}
helper.setResponse(buildings);
}

@Override
public void doDelete(ApiHelper helper) throws IOException {
Transaction tx = helper.getHibSession().beginTransaction();
try {
Building building = null;
Long buildingId = helper.getOptinalParameterLong("id", null);
if (buildingId != null) {
building = BuildingDAO.getInstance().get(buildingId, helper.getHibSession());
if (building == null)
throw new IllegalArgumentException("Building " + buildingId + " does not exist.");
} else {
Long sessionId = helper.getAcademicSessionId();
if (sessionId == null)
throw new IllegalArgumentException("Academic session not provided, please set the term parameter.");
String externalId = helper.getOptinalParameter("externalId", null);
if (externalId != null) {
building = (Building)helper.getHibSession().createQuery("from Building where externalUniqueId = :externalId and session.uniqueId = :sessionId")
.setLong("sessionId", sessionId).setString("externalId", externalId).setMaxResults(1).uniqueResult();
if (building == null)
throw new IllegalArgumentException("Building " + externalId + " does not exist.");
}
if (building == null) {
String abbv = helper.getRequiredParameter("building");
building = (Building)helper.getHibSession().createQuery("from Building where (abbreviation = :abbv or name = :abbv) and session.uniqueId = :sessionId")
.setLong("sessionId", sessionId).setString("abbv", abbv).setMaxResults(1).uniqueResult();
if (building == null)
throw new IllegalArgumentException("Building " + abbv + " does not exist.");
}
}
helper.getSessionContext().checkPermissionAnyAuthority(building.getSession(), Right.ApiRoomEdit);
helper.getSessionContext().checkPermissionAnyAuthority(building, Right.BuildingDelete);

for (Room r: (List<Room>)BuildingDAO.getInstance().getSession().createQuery("from Room r where r.building.uniqueId = :buildingId").setLong("buildingId", building.getUniqueId()).list()) {
helper.getHibSession().createQuery("delete RoomPref p where p.room.uniqueId = :roomId").setLong("roomId", r.getUniqueId()).executeUpdate();
for (Iterator<Assignment> i = r.getAssignments().iterator(); i.hasNext(); ) {
Assignment a = i.next();
a.getRooms().remove(r);
helper.getHibSession().saveOrUpdate(a);
i.remove();
}
helper.getHibSession().delete(r);
}
ChangeLog.addChange(
helper.getHibSession(),
TimetableManager.findByExternalId(sessionContext.getUser().getExternalUserId()),
building.getSession(),
building,
ChangeLog.Source.BUILDING_EDIT,
ChangeLog.Operation.DELETE,
null,
null);
helper.getHibSession().delete(building);
tx.commit();
} catch (Exception e) {
if (tx != null) { tx.rollback(); }
if (e instanceof RuntimeException) throw (RuntimeException)e;
if (e instanceof IOException) throw (IOException)e;
throw new IOException(e.getMessage(), e);
}
}


@Override
public void doPost(ApiHelper helper) throws IOException {
BuildingInterface b = helper.getRequest(BuildingInterface.class);
Transaction tx = helper.getHibSession().beginTransaction();
try {
Building building = null;
if (b.getId() != null) {
building = BuildingDAO.getInstance().get(b.getId(), helper.getHibSession());
if (building == null)
throw new IllegalArgumentException("Building " + b.getId() + " does not exist.");
} else {
Long sessionId = helper.getAcademicSessionId();
if (sessionId == null)
throw new IllegalArgumentException("Academic session not provided, please set the term parameter.");
if (b.getExternalId() != null) {
building = (Building)helper.getHibSession().createQuery("from Building where externalUniqueId = :externalId and session.uniqueId = :sessionId")
.setLong("sessionId", sessionId).setString("externalId", b.getExternalId()).setMaxResults(1).uniqueResult();
} else if (b.getAbbreviation() != null) {
building = (Building)helper.getHibSession().createQuery("from Building where abbreviation = :abbv and session.uniqueId = :sessionId")
.setLong("sessionId", sessionId).setString("abbv", b.getAbbreviation()).setMaxResults(1).uniqueResult();
}
}
if (building != null) {
helper.getSessionContext().checkPermissionAnyAuthority(building.getSession(), Right.ApiRoomEdit);
helper.getSessionContext().checkPermissionAnyAuthority(building, Right.BuildingEdit);
} else {
helper.getSessionContext().checkPermissionAnyAuthority(helper.getAcademicSessionId(), "Session", Right.ApiRoomEdit);
helper.getSessionContext().checkPermissionAnyAuthority(helper.getAcademicSessionId(), "Session", Right.BuildingAdd);
}

ChangeLog.Operation op = null;
if (building == null) {
building = new Building();
building.setSession(SessionDAO.getInstance().get(helper.getAcademicSessionId(), helper.getHibSession()));
op = ChangeLog.Operation.CREATE;
} else {
op = ChangeLog.Operation.UPDATE;
}
building.setName(b.getName());
building.setAbbreviation(b.getAbbreviation());
building.setExternalUniqueId(b.getExternalId());
building.setCoordinateX(b.getX());
building.setCoordinateY(b.getY());
helper.getHibSession().saveOrUpdate(building);

b.setId(building.getUniqueId());

ChangeLog.addChange(
helper.getHibSession(),
TimetableManager.findByExternalId(sessionContext.getUser().getExternalUserId()),
building.getSession(),
building,
ChangeLog.Source.BUILDING_EDIT,
op,
null,
null);
tx.commit();
} catch (Exception e) {
if (tx != null) { tx.rollback(); }
if (e instanceof RuntimeException) throw (RuntimeException)e;
if (e instanceof IOException) throw (IOException)e;
throw new IOException(e.getMessage(), e);
}
helper.setResponse(b);
}

@Override
protected String getName() {
return "buildings";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ public static class BuildingInterface implements GwtRpcResponse {
private String iAbbreviation;
private String iName;
private Double iX, iY;
private String iExternalId;

public BuildingInterface() {}

Expand All @@ -819,6 +820,10 @@ public BuildingInterface(Long id, String abbreviation, String name) {
public Double getY() { return iY; }
public void setY(Double y) { iY = y; }

public boolean hasExternalId() { return iExternalId != null && !iExternalId.isEmpty(); }
public String getExternalId() { return iExternalId; }
public void setExternalId(String externalId) { iExternalId = externalId; }

@Override
public int hashCode() { return getId().hashCode(); }

Expand Down
4 changes: 2 additions & 2 deletions JavaSource/org/unitime/timetable/model/Building.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public int compareTo(Object o) {
* @return
* @throws Exception
*/
public static Building findByBldgAbbv(String bldgAbbv, Long sessionId) throws Exception {
public static Building findByBldgAbbv(String bldgAbbv, Long sessionId) {
List bldgs = (new BuildingDAO()).getQuery(
"SELECT distinct b FROM Building b "+
"WHERE b.session.uniqueId=:sessionId AND b.abbreviation=:bldgAbbv").
Expand All @@ -111,7 +111,7 @@ public static Building findByBldgAbbv(String bldgAbbv, Long sessionId) throws Ex
return null;
}

public static Building findByName(String name, Long sessionId) throws Exception {
public static Building findByName(String name, Long sessionId) {
return (Building)(new BuildingDAO()).getSession().createQuery(
"select b from Building b where b.session.uniqueId=:sessionId and b.name=:name").
setLong("sessionId", sessionId.longValue()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ protected RoomDetailInterface load(Location location, String department, boolean
Building b = room.getBuilding();
BuildingInterface building = new BuildingInterface(b.getUniqueId(), b.getAbbreviation(), b.getName());
building.setX(b.getCoordinateX()); building.setY(b.getCoordinateY());
building.setExternalId(b.getExternalUniqueId());
response.setBuilding(building);
response.setName(room.getRoomNumber());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public RoomPropertiesInterface execute(RoomPropertiesRequest request, SessionCon
for (Building b: Building.findAll(response.getAcademicSessionId())) {
BuildingInterface building = new BuildingInterface(b.getUniqueId(), b.getAbbreviation(), b.getName());
building.setX(b.getCoordinateX()); building.setY(b.getCoordinateY());
building.setExternalId(b.getExternalUniqueId());
response.addBuilding(building);
}

Expand Down
14 changes: 14 additions & 0 deletions WebContent/help/Release-Notes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@
</line>
</description>
</item>
<item>
<name>API: Buildings</name>
<description>
<line>Added buildings API that can
<line>return list of buildings for an academic session (GET api/buildings?term=&lt;TERM&gt; or GET api/buildings?sessionId=&lt;TERM ID&gt;)</line>
<line>creature or update a building (using POST request with the building as a payload)</line>
<line>delete a building (DELETE api/buildings?id=&lt;BUILDING ID&gt; or DELETE api/buildings?term=&lt;TERM&gt;&amp;externalId=&lt;EXTID&gt; or DELETE api/buildings?term=&lt;TERM&gt;&amp;building=&lt;ABBV&gt;)</line>
</line>
<line>Permissions:
<line>ApiRetrieveRooms to retrieve list of buildings (GET request)</line>
<line>ApiRoomEdit to create, update, and delete a room, together with the appropriate BuildingAdd, BuildingEdit and BuildingDelete permission (POST and DELETE requests)</line>
</line>
</description>
</item>
</category>
</release>

Expand Down

0 comments on commit 9b19bfa

Please sign in to comment.