@Override public String advancedPut(Session s, String[] args, String content) throws WPISuiteException { Project p = getEntity(args[2])[0]; String[] names = null; try { names = gson.fromJson(content, String[].class); } catch (JsonSyntaxException j) { throw new BadRequestException("Could not parse JSON"); } ArrayList<String> success = new ArrayList<String>(); UserManager u = ManagerLayer.getInstance().getUsers(); if (args.length > 3) { if ("add".equals(args[3])) { for (String person : names) { if (p.addTeamMember(u.getEntity(s, person)[0])) success.add(person); } } else if ("remove".equals(args[3])) { for (String person : names) { if (p.removeTeamMember(u.getEntity(s, person)[0])) success.add(person); } } } return gson.toJson(success.toArray(names), String[].class); }
@Override public Project makeEntity(Session s, String content) throws WPISuiteException { User theUser = s.getUser(); logger.log(Level.FINER, "Attempting new Project creation..."); Project p; try { p = Project.fromJSON(content); } catch (JsonSyntaxException e) { logger.log(Level.WARNING, "Invalid Project entity creation string."); throw new BadRequestException( "The entity creation string had invalid format. Entity String: " + content); } logger.log(Level.FINE, "New project: " + p.getName() + " submitted by: " + theUser.getName()); p.setOwner(theUser); if (getEntity(s, p.getIdNum())[0] == null) { if (getEntityByName(s, p.getName())[0] == null) { save(s, p); } else { logger.log(Level.WARNING, "Project Name Conflict Exception during Project creation."); throw new ConflictException( "A project with the given name already exists. Entity String: " + content); } } else { logger.log(Level.WARNING, "ID Conflict Exception during Project creation."); throw new ConflictException( "A project with the given ID already exists. Entity String: " + content); } logger.log(Level.FINER, "Project creation success!"); return p; }
public Project update(Session s, Project toUpdate, String changeSet) throws WPISuiteException { if (s == null) { throw new WPISuiteException("Null session."); } User theUser = s.getUser(); if (theUser.equals(toUpdate.getOwner()) || theUser.getRole().equals(Role.ADMIN)) { // convert updateString into a Map, then load into the User try { logger.log(Level.FINE, "Project update being attempted..."); Project change = Project.fromJSON(changeSet); // check if the changes contains each field of name if (change.getName() != null && !change.getName().equals("")) { // check for conflict for changing the project name Project isConflict = getEntityByName(s, change.getName())[0]; if (isConflict != null && !isConflict.getIdNum().equals(change.getIdNum())) { throw new ConflictException( "ProjectManager attempted to update a Project's name to be the same as an existing project"); } toUpdate.setName(change.getName()); } if (change.getOwner() != null) { toUpdate.setOwner(change.getOwner()); } } catch (ConflictException e) { logger.log( Level.WARNING, "ProjectManager attempted to update a Project's name to be the same as an existing project"); throw e; } catch (Exception e) { logger.log(Level.WARNING, "ProjectManager.update() had a failure in the changeset mapper."); throw new DatabaseException( "Failure in the ProjectManager.update() changeset mapper."); // on Mapping failure } // save the changes back this.save(s, toUpdate); // check for changes in each field return toUpdate; } else { logger.log(Level.WARNING, "Unauthorized Project update attempted."); throw new UnauthorizedException( "You do not have the required permissions to perform this action."); } }