/*
   * (non-Javadoc)
   *
   * @see ca.unb.cs.pcsf.db.DBAccessService#updateCollaborationState(ca.unb.cs.pcsf.db.Collaboration,
   * java.lang.String)
   */
  public void updateCollaborationState(Collaboration collaboration, String state) {
    logger.debug(LOGPRE + "updateCollaborationState() start" + LOGPRE);

    logger.info("Updating collaboration <" + collaboration.getName() + "> state to be" + state);
    String domainName = DOMAIN_COLLABORATION;
    String itemName = collaboration.getId();
    List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>();

    replaceableAttributes.add(
        new ReplaceableAttribute(COLLABORATION_ATTRIBUTE_CURRENT_STATE, state, true));

    sdb.putAttributes(new PutAttributesRequest(domainName, itemName, replaceableAttributes));

    logger.debug(LOGPRE + "updateCollaborationState() end" + LOGPRE);
  }
  /*
   * (non-Javadoc)
   *
   * @see ca.unb.cs.pcsf.db.DBAccessService#update(java.lang.Object)
   */
  public void update(Object o) {
    logger.debug(LOGPRE + "update() start" + LOGPRE);

    if (o instanceof Collaboration) {
      Collaboration collaboration = (Collaboration) o;
      logger.info("Updating Collaboration <" + collaboration.getName() + ">...");

      String domainName = DOMAIN_COLLABORATION;
      String itemName = collaboration.getId();
      List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>();

      replaceableAttributes.add(
          new ReplaceableAttribute(COLLABORATION_ATTRIBUTE_NAME, collaboration.getName(), true));
      for (Participant s : collaboration.getParticipants())
        replaceableAttributes.add(
            new ReplaceableAttribute(COLLABORATION_ATTRIBUTE_PARTICIPANT, s.getName(), true));

      sdb.putAttributes(new PutAttributesRequest(domainName, itemName, replaceableAttributes));
    }

    if (o instanceof Participant) {
      Participant participant = (Participant) o;
      logger.info("Updating participant <" + participant.getName() + ">...");

      String domainName = DOMAIN_PARTICIPANT;
      String itemName = participant.getId();
      List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>();

      replaceableAttributes.add(
          new ReplaceableAttribute(PARTICIPANT_ATTRIBUTE_IS_REG, participant.getIsReg(), true));

      sdb.putAttributes(new PutAttributesRequest(domainName, itemName, replaceableAttributes));
    }

    logger.debug(LOGPRE + "update() end" + LOGPRE);
  }
  /*
   * (non-Javadoc)
   *
   * @see ca.unb.cs.pcsf.db.DBAccessService#deleteCollaboration(ca.unb.cs.pcsf.db.Collaboration)
   */
  public void deleteCollaboration(Collaboration collaboration) {
    logger.debug(LOGPRE + "deleteCollaboration() start" + LOGPRE);

    if (isCollaborationExist(collaboration.getName())) {
      logger.info("Deleting participants...");
      List<Participant> participants = collaboration.getParticipants();
      for (Participant p : participants) {
        sdb.deleteAttributes(new DeleteAttributesRequest(DOMAIN_PARTICIPANT, p.getId()));
      }
      sdb.deleteAttributes(
          new DeleteAttributesRequest(DOMAIN_COLLABORATION, collaboration.getId()));

      logger.info("Delete Done!");
      logger.debug(LOGPRE + "deleteCollaboration() end" + LOGPRE);
    }
  }
  /*
   * (non-Javadoc)
   *
   * @see ca.unb.cs.pcsf.db.DBAccessService#putDataIntoDomain(java.lang.Object)
   */
  public void putDataIntoDomain(Object object) {
    logger.debug(LOGPRE + "putDataIntoDomain() start" + LOGPRE);

    if (object instanceof Participant) {
      Participant participant = (Participant) object;
      List<ReplaceableItem> items = new ArrayList<ReplaceableItem>();
      items.add(
          new ReplaceableItem(participant.getId())
              .withAttributes(
                  new ReplaceableAttribute(PARTICIPANT_ATTRIBUTE_NAME, participant.getName(), true),
                  new ReplaceableAttribute(
                      PARTICIPANT_ATTRIBUTE_EMAIL, participant.getEmail(), true),
                  new ReplaceableAttribute(
                      PARTICIPANT_ATTRIBUTE_COLLABORATION_ID,
                      participant.getCollaborationId(),
                      false),
                  new ReplaceableAttribute(
                      PARTICIPANT_ATTRIBUTE_IS_REG, participant.getIsReg(), true)));

      logger.info("Putting participant <" + participant.getName() + "> into domain...");
      sdb.batchPutAttributes(new BatchPutAttributesRequest(DOMAIN_PARTICIPANT, items));
    }

    if (object instanceof Creator) {
      Creator creator = (Creator) object;
      if (!isCreatorExist(creator.getName())) {
        List<ReplaceableItem> items = new ArrayList<ReplaceableItem>();
        items.add(
            new ReplaceableItem(creator.getId())
                .withAttributes(
                    new ReplaceableAttribute(CREATOR_ATTRIBUTE_NAME, creator.getName(), true),
                    new ReplaceableAttribute(
                        CREATOR_ATTRIBUTE_PASSWORD, creator.getPassword(), true),
                    new ReplaceableAttribute(CREATOR_ATTRIBUTE_EMAIL, creator.getEmail(), true)));

        logger.info("Putting creator <" + creator.getName() + "> into domain...");
        sdb.batchPutAttributes(new BatchPutAttributesRequest(DOMAIN_CREATOR, items));
      }
    }

    if (object instanceof Collaboration) {
      Collaboration collaboration = (Collaboration) object;

      if (!isCollaborationExist(collaboration.getName())) {
        List<ReplaceableItem> items = new ArrayList<ReplaceableItem>();
        ReplaceableItem item = new ReplaceableItem(collaboration.getId());
        item.withAttributes(
            new ReplaceableAttribute(COLLABORATION_ATTRIBUTE_NAME, collaboration.getName(), true),
            new ReplaceableAttribute(
                COLLABORATION_ATTRIBUTE_CREATOR_ID, collaboration.getCreatorId(), true),
            new ReplaceableAttribute(
                COLLABORATION_ATTRIBUTE_CURRENT_STATE, collaboration.getCurrentState(), true),
            new ReplaceableAttribute(
                COLLABORATION_ATTRIBUTE_WORKFLOW_MODEL, collaboration.getWorkflowModel(), true));

        List<Participant> participants = collaboration.getParticipants();
        for (Participant participant : participants)
          item.withAttributes(
              new ReplaceableAttribute(
                  COLLABORATION_ATTRIBUTE_PARTICIPANT, participant.getName(), true));

        items.add(item);

        logger.info("Putting collaboration <" + collaboration.getName() + "> into domain...");
        sdb.batchPutAttributes(new BatchPutAttributesRequest(DOMAIN_COLLABORATION, items));
      }
    }

    logger.debug(LOGPRE + "putDataIntoDomain() end" + LOGPRE);
  }