/*
   * Saves a Commitment when it is received from a client
   *
   * @see edu.wpi.cs.wpisuitetng.modules.EntityManager#makeEntity(edu.wpi.cs.wpisuitetng.Session, java.lang.String)
   */
  @Override
  public Commitment makeEntity(Session s, String content)
      throws BadRequestException, ConflictException, WPISuiteException {

    // Parse the message from JSON
    final Commitment newMessage = Commitment.fromJSON(content);

    // System.out.println("EM: marked for delete:" + newMessage.isMarkedForDeletion());

    newMessage.setOwnerName(s.getUsername());
    newMessage.setOwnerID(s.getUser().getIdNum());

    // Until we find a id that is unique assume another commitment might already have it
    boolean unique;
    long id = 0;
    do {
      unique = true;
      id = UUID.randomUUID().getMostSignificantBits();
      for (Commitment c : this.getAll(s)) if (c.getUniqueID() == id) unique = false;
    } while (!unique);

    newMessage.setUniqueID(id);
    System.out.printf(
        "Server: Creating new commitment with id = %s and owner = %s\n",
        newMessage.getUniqueID(), newMessage.getOwnerName());

    // Save the message in the database if possible, otherwise throw an exception
    // We want the message to be associated with the project the user logged in to
    if (!db.save(newMessage, s.getProject())) {
      throw new WPISuiteException();
    }

    // Return the newly created message (this gets passed back to the client)
    return newMessage;
  }
  /*
   * Commitment cannot be updated. This method always throws an exception.
   *
   * @see edu.wpi.cs.wpisuitetng.modules.EntityManager#update(edu.wpi.cs.wpisuitetng.Session, java.lang.String)
   */
  @Override
  public Commitment update(Session s, String content) throws WPISuiteException {
    Commitment updatedCommitment = Commitment.fromJSON(content);
    /*
     * Because of the disconnected objects problem in db4o, we can't just save Commitments.
     * We have to get the original defect from db4o, copy properties from updatedCommitment,
     * then save the original Commitment again.
     */
    List<Model> oldCommitments =
        db.retrieve(Commitment.class, "id", updatedCommitment.getID(), s.getProject());
    // System.out.println(oldCommitments.toString());
    if (oldCommitments.size() < 1 || oldCommitments.get(0) == null) {
      throw new BadRequestException("Commitment with ID does not exist.");
    }

    Commitment existingCommitment = (Commitment) oldCommitments.get(0);

    // copy values to old commitment and fill in our changeset appropriately
    existingCommitment.copyFrom(updatedCommitment);

    if (!db.save(existingCommitment, s.getProject())) {
      throw new WPISuiteException();
    }

    return existingCommitment;
  }
  @Override
  public void save(Session s, Project model) throws WPISuiteException {
    if (s == null) {
      throw new WPISuiteException("Null Session.");
    }
    // permissions checking happens in update, create, and delete methods only
    /*User theUser = s.getUser();
    if(Role.ADMIN.equals(theUser.getRole()) ||
    		Permission.WRITE.equals(model.getPermission(theUser))){*/
    if (data.save(model)) {
      logger.log(Level.FINE, "Project Saved :" + model);
      return;
    }
    /*else
    {
    	logger.log(Level.WARNING, "Project Save Failure!");
    	throw new DatabaseException("Save failure for Project."); // Session User: "******" Project: " + model.getName());
    }
    /*}
    else
    {
    	logger.log(Level.WARNING, "ProjectManager Save attempted by user with insufficient permission");
    	throw new UnauthorizedException("You do not have the requred permissions to perform this action.");
    }*/

  }
 /*
  * @see edu.wpi.cs.wpisuitetng.modules.EntityManager#save(edu.wpi.cs.wpisuitetng.Session, edu.wpi.cs.wpisuitetng.modules.Model)
  */
 @Override
 public void save(Session s, Commitment model) throws WPISuiteException {
   db.save(model, s.getProject());
 }