private void saveNotes(
     String execIdentifier,
     Integer objectId,
     String notes,
     ObjNote objnote,
     String owner,
     String visibility,
     IEngUserProfile profile)
     throws Exception {
   logger.debug("IN");
   try {
     IObjNoteDAO objNoteDAO = DAOFactory.getObjNoteDAO();
     objNoteDAO.setUserProfile(profile);
     if (objnote != null) {
       // Modify note
       objnote.setContent(notes.getBytes());
       objnote.setIsPublic((visibility.equalsIgnoreCase("PUBLIC") ? true : false));
       objNoteDAO.modifyExecutionNotes(objnote);
     } else {
       // Insert new note
       objnote = new ObjNote();
       objnote.setContent(notes.getBytes());
       objnote.setExecReq(execIdentifier);
       objnote.setIsPublic((visibility.equalsIgnoreCase("PUBLIC") ? true : false));
       objnote.setOwner(owner);
       objNoteDAO.saveExecutionNotes(objectId, objnote);
     }
   } finally {
     logger.debug("OUT");
   }
 }
  public void doService() {
    logger.debug("IN");
    try {
      // retrieving execution instance from session, no need to check if user is able to execute the
      // current document
      ExecutionInstance executionInstance =
          getContext().getExecutionInstance(ExecutionInstance.class.getName());
      BIObject obj = executionInstance.getBIObject();
      BIObjectNotesManager objectNManager = new BIObjectNotesManager();
      String execIdentifier = objectNManager.getExecutionIdentifier(obj);

      String previousNotes = this.getAttributeAsString("PREVIOUS_NOTES");
      logger.debug("Parameter [" + PREVIOUS_NOTES + "] is equal to [" + previousNotes + "]");

      String notes = this.getAttributeAsString("NOTES");
      logger.debug("Parameter [" + NOTES + "] is equal to [" + notes + "]");

      String message = this.getAttributeAsString(MESSAGE);
      logger.debug("Parameter [" + MESSAGE + "] is equal to [" + message + "]");

      String visibility = this.getAttributeAsString(VISIBILITY);
      logger.debug("Parameter [" + VISIBILITY + "] is equal to [" + visibility + "]");

      String resultStr = null;

      ObjNote objnote = null;

      SessionContainer sessCont = getSessionContainer();
      SessionContainer permCont = sessCont.getPermanentContainer();
      IEngUserProfile profile =
          (IEngUserProfile) permCont.getAttribute(IEngUserProfile.ENG_USER_PROFILE);

      String owner = (String) ((UserProfile) profile).getUserId();
      try {
        objnote =
            DAOFactory.getObjNoteDAO().getExecutionNotesByOwner(obj.getId(), execIdentifier, owner);
      } catch (Exception e) {
        logger.error(
            "Cannot load notes for document [id: "
                + obj.getId()
                + ", label: "
                + obj.getLabel()
                + ", name: "
                + obj.getName()
                + "]",
            e);
        throw new SpagoBIServiceException(SERVICE_NAME, "Cannot load notes", e);
      }
      String currentNotes = "";
      if (objnote != null) {
        logger.debug("Existing notes found with the same execution identifier");
        byte[] content = objnote.getContent();
        currentNotes = new String(content);
      }
      if (!"INSERT_NOTE".equalsIgnoreCase(MESSAGE) && !currentNotes.equals(previousNotes)) {
        logger.debug("Notes have been created by another user");
        resultStr = "conflict";
      } else {
        logger.debug("Saving notes...");
        try {
          saveNotes(execIdentifier, obj.getId(), notes, objnote, owner, visibility, profile);
          logger.debug("Notes saved");
          resultStr = "ok";
        } catch (Exception e) {
          throw new SpagoBIServiceException(SERVICE_NAME, "Error while saving notes", e);
        }
      }

      try {
        JSONObject result = new JSONObject();
        result.put("result", resultStr);
        writeBackToClient(new JSONSuccess(result));
      } catch (IOException e) {
        throw new SpagoBIServiceException(
            SERVICE_NAME, "Impossible to write back the responce to the client", e);
      } catch (JSONException e) {
        throw new SpagoBIServiceException(
            SERVICE_NAME, "Cannot serialize objects into a JSON object", e);
      }
    } finally {
      logger.debug("OUT");
    }
  }