public static void __getNotes(String url, String token, Long[] ids)
     throws MoodleRestNotesException, MoodleRestException, UnsupportedEncodingException {
   if (MoodleCallRestWebService.isLegacy())
     throw new MoodleRestNotesException(MoodleRestException.NO_LEGACY);
   // MoodleWarning[] warnings=null;
   String functionCall = MoodleServices.CORE_NOTES_GET_NOTES.toString();
   StringBuilder data = new StringBuilder();
   data.append(URLEncoder.encode("wstoken", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode(token, MoodleServices.ENCODING.toString()));
   data.append("&")
       .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
   for (int i = 0; i < ids.length; i++) {
     if (ids[i] == null) throw new MoodleRestNotesException();
     else
       data.append("&")
           .append(URLEncoder.encode("notes[" + i + "]", MoodleServices.ENCODING.toString()))
           .append("=")
           .append(ids[i]);
   }
   data.trimToSize();
   NodeList elements = (new MoodleCallRestWebService()).__call(url, data.toString());
   // return warnings;
 }
 /**
  * Method to attach notes to users.
  *
  * @param notes MoodleNote[]
  * @return MoodleNote[]
  * @throws MoodleRestNotesException
  * @throws MoodleRestException
  */
 public static MoodleNote[] createNotes(MoodleNote[] notes)
     throws MoodleRestNotesException, MoodleRestException {
   int processedCount = 0;
   String functionCall =
       MoodleCallRestWebService.isLegacy()
           ? MoodleServices.MOODLE_NOTES_CREATE_NOTES.toString()
           : MoodleServices.CORE_NOTES_CREATE_NOTES.toString();
   try {
     StringBuilder data = new StringBuilder();
     if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestNotesException();
     else data.append(MoodleCallRestWebService.getAuth());
     data.append("&")
         .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
     for (int i = 0; i < notes.length; i++) {
       if (notes[i] == null)
         throw new MoodleRestNotesException(MoodleRestNotesException.NOTES_NULL);
       if (notes[i].getUserId() == null)
         throw new MoodleRestNotesException(MoodleRestNotesException.USERID_NOT_SET);
       else
         data.append("&")
             .append(
                 URLEncoder.encode("notes[" + i + "][userid]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(
                 URLEncoder.encode("" + notes[i].getUserId(), MoodleServices.ENCODING.toString()));
       if (notes[i].getPublishState() == null)
         throw new MoodleRestNotesException(MoodleRestNotesException.PUBLISHSTATE_NULL);
       else
         data.append("&")
             .append(
                 URLEncoder.encode(
                     "notes[" + i + "][publishstate]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(
                 URLEncoder.encode(
                     notes[i].getPublishState(), MoodleServices.ENCODING.toString()));
       if (notes[i].getCourseId() == null)
         throw new MoodleRestNotesException(MoodleRestNotesException.COURSEID_NOT_SET);
       else
         data.append("&")
             .append(
                 URLEncoder.encode(
                     "notes[" + i + "][courseid]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(
                 URLEncoder.encode(
                     "" + notes[i].getCourseId(), MoodleServices.ENCODING.toString()));
       if (notes[i].getText() == null)
         throw new MoodleRestNotesException(MoodleRestNotesException.TEXT_NULL);
       else
         data.append("&")
             .append(
                 URLEncoder.encode("notes[" + i + "][text]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(URLEncoder.encode(notes[i].getText(), MoodleServices.ENCODING.toString()));
       if (notes[i].getFormat() == null) {
         notes[i].setFormat("text");
       }
       if (notes[i].getFormat().equals("text") || notes[i].getFormat().equals("html"))
         data.append("&")
             .append(
                 URLEncoder.encode("notes[" + i + "][format]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(URLEncoder.encode(notes[i].getFormat(), MoodleServices.ENCODING.toString()));
       else throw new MoodleRestNotesException(MoodleRestNotesException.FORMAT_INCORRECT);
       if (notes[i].getClientNoteId() != null)
         data.append("&")
             .append(
                 URLEncoder.encode(
                     "notes[" + i + "][clientnoteid]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(
                 URLEncoder.encode(
                     notes[i].getClientNoteId(), MoodleServices.ENCODING.toString()));
     }
     data.trimToSize();
     NodeList elements = MoodleCallRestWebService.call(data.toString());
     for (int j = 0; j < elements.getLength(); j += 3, processedCount++) {
       for (int k = 0; k < 3; k++) {
         String content = elements.item(j + k).getTextContent();
         String nodeName =
             elements
                 .item(j + k)
                 .getParentNode()
                 .getAttributes()
                 .getNamedItem("name")
                 .getNodeValue();
         notes[processedCount].setMoodleNoteField(nodeName, content);
       }
     }
   } catch (UnsupportedEncodingException ex) {
     Logger.getLogger(MoodleRestNotes.class.getName()).log(Level.SEVERE, null, ex);
   }
   return notes;
 }
 public static void updateNotes(MoodleNote[] notes, MoodleWarning[] warnings)
     throws MoodleRestNotesException, UnsupportedEncodingException, MoodleRestException {
   if (MoodleCallRestWebService.isLegacy())
     throw new MoodleRestNotesException(MoodleRestException.NO_LEGACY);
   String functionCall = MoodleServices.CORE_NOTES_UPDATE_NOTES.toString();
   StringBuilder data = new StringBuilder();
   if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestNotesException();
   else data.append(MoodleCallRestWebService.getAuth());
   data.append("&")
       .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
   for (int i = 0; i < notes.length; i++) {
     data.append("&")
         .append(URLEncoder.encode("notes[" + i + "][id]", MoodleServices.ENCODING.toString()))
         .append("=")
         .append("" + notes[i].getNoteId());
     data.append("&")
         .append(
             URLEncoder.encode(
                 "notes[" + i + "][publishstate]", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(notes[i].getPublishState());
     data.append("&")
         .append(URLEncoder.encode("notes[" + i + "][text]", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(notes[i].getText());
     data.append("&")
         .append(URLEncoder.encode("notes[" + i + "][format]", MoodleServices.ENCODING.toString()))
         .append("=")
         .append("" + notes[i].getDescriptionFormat().toInt());
   }
   data.trimToSize();
   NodeList elements = MoodleCallRestWebService.call(data.toString());
   ArrayList<MoodleWarning> warn = null;
   MoodleWarning warning = null;
   for (int j = 0; j < elements.getLength(); j++) {
     String parent =
         elements
             .item(j)
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getAttributes()
             .getNamedItem("name")
             .getNodeValue();
     String content = elements.item(j).getTextContent();
     String nodeName =
         elements.item(j).getParentNode().getAttributes().getNamedItem("name").getNodeValue();
     if (parent.equals("warnings")) {
       if (nodeName.equals("item")) {
         if (warn == null) {
           warn = new ArrayList<MoodleWarning>();
         }
         warning = new MoodleWarning(content);
         warn.add(warning);
       } else {
         warning.setMoodleWarningField(nodeName, content);
       }
     }
   }
   if (warn != null) {
     if (warnings != null) {
       warnings = new MoodleWarning[warn.size()];
       warnings = warn.toArray(warnings);
     }
   }
 }
  /**
   * Method to that perform actions on the contacts provided a list of ids
   *
   * @throws MoodleRestMessageException
   * @throws MoodleRestException
   * @throws UnsupportedEncodingException
   */
  public static void actionContacts(Long[] contacts, MoodleContactAction action)
      throws MoodleRestMessageException, UnsupportedEncodingException, MoodleRestException {
    String functionCall = null;
    StringBuilder data = new StringBuilder();

    if (MoodleCallRestWebService.isLegacy())
      throw new MoodleRestException(MoodleRestException.NO_LEGACY);
    else {
      switch (action) {
        case DELETE:
          functionCall = MoodleServices.CORE_MESSAGE_DELETE_CONTACTS.name();
          break;
        case CREATE:
          functionCall = MoodleServices.CORE_MESSAGE_CREATE_CONTACTS.name();
          break;
        case BLOCK:
          functionCall = MoodleServices.CORE_MESSAGE_BLOCK_CONTACTS.name();
          break;
        case UNBLOCK:
          functionCall = MoodleServices.CORE_MESSAGE_UNBLOCK_CONTACTS.name();
          break;
      }
    }

    try {
      if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestUserException();
      else data.append(MoodleCallRestWebService.getAuth());

      data.append("&");
      data.append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()));
      data.append("=");
      data.append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString().toString()));

      for (int i = 0; i < contacts.length; i++) {
        if (contacts[i] < 1) {
          throw new MoodleRestMessageException(MoodleRestMessageException.PARAMETER_RANGE);
        } else {
          data.append("&");
          data.append(URLEncoder.encode("userids[" + i + "]", MoodleServices.ENCODING.toString()));
          data.append("=");
          data.append(contacts[i]);
        }
      }

      data.trimToSize();
      MoodleCallRestWebService.call(data.toString());

    } catch (final IOException ex) {
      Logger.getLogger(MoodleRestMessage.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
 public static MoodleGradeArea[] getGradeDefinitions(
     Long[] cmIds, String areaName, boolean activeOnly, MoodleWarning[] warnings)
     throws MoodleRestGradeException, UnsupportedEncodingException, MoodleRestException {
   if (MoodleCallRestWebService.isLegacy()) {
     throw new MoodleRestGradeException(MoodleRestException.NO_LEGACY);
   }
   StringBuilder data = new StringBuilder();
   String functionCall = MoodleServices.CORE_GRADING_GET_DEFINITIONS.toString();
   if (MoodleCallRestWebService.getAuth() == null) {
     throw new MoodleRestGradeException();
   } else {
     data.append(MoodleCallRestWebService.getAuth());
   }
   data.append("&")
       .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
   for (int i = 0; i < cmIds.length; i++) {
     data.append("&")
         .append(URLEncoder.encode("cmids[" + i + "]", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(URLEncoder.encode("" + cmIds[i], MoodleServices.ENCODING.toString()));
   }
   data.append("&")
       .append(URLEncoder.encode("areaname", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode("" + areaName, MoodleServices.ENCODING.toString()));
   if (activeOnly) {
     data.append("&")
         .append(URLEncoder.encode("activeonly", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(URLEncoder.encode("" + (activeOnly ? 1 : 0), MoodleServices.ENCODING.toString()));
   }
   data.trimToSize();
   NodeList elements = MoodleCallRestWebService.call(data.toString());
   ArrayList<MoodleGradeArea> areas = null;
   ArrayList<MoodleWarning> warn = null;
   MoodleGradeArea gradeArea = null;
   MoodleGradeArea.GradeDefinition definition = null;
   MoodleGradeArea.GradeDefinition.Rubric.RubricCriteria rubricCriteria = null;
   Level level = null;
   Guide guide = null;
   GuideCriteria guideCriteria = null;
   GuideComment guideComment = null;
   MoodleWarning warning = null;
   for (int j = 0; j < elements.getLength(); j++) {
     String parent =
         elements
             .item(j)
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getAttributes()
             .getNamedItem("name")
             .getNodeValue();
     String content = elements.item(j).getTextContent();
     String nodeName =
         elements.item(j).getParentNode().getAttributes().getNamedItem("name").getNodeValue();
     if (parent.equals("areas")) {
       if (nodeName.equals("cmid")) {
         if (areas == null) {
           areas = new ArrayList<MoodleGradeArea>();
         }
         gradeArea = new MoodleGradeArea();
         areas.add(gradeArea);
         gradeArea.setCmid(Long.parseLong(content));
       } else {
         gradeArea.setFieldValue(nodeName, content);
       }
     } else {
       if (parent.equals("definitions")) {
         if (nodeName.equals("id")) {
           definition = gradeArea.newGradeDefinition();
           definition.setId(Long.parseLong(content));
         } else {
           definition.setFieldValue(nodeName, content);
         }
       } else {
         if (parent.equals("rubric_criteria")) {
           if (nodeName.equals("id")) {
             rubricCriteria = definition.getRubric().newRubricCriteria();
             rubricCriteria.setId(Long.parseLong(content));
           } else {
             rubricCriteria.setFieldValue(nodeName, content);
           }
         } else {
           if (parent.equals("levels")) {
             if (nodeName.equals("id")) {
               level = rubricCriteria.newLevel();
               level.setId(Long.parseLong(content));
             } else {
               level.setFieldValue(nodeName, content);
             }
           } else {
             if (parent.equals("guide_criteria")) {
               if (nodeName.equals("id")) {
                 if (guide == null) {
                   guide = definition.newGuide();
                 }
                 guideCriteria = guide.newGuideCriteria();
                 guideCriteria.setId(Long.parseLong(content));
               } else {
                 guideCriteria.setFieldValue(nodeName, content);
               }
             } else {
               if (parent.equals("guide_comment")) {
                 if (nodeName.equals("id")) {
                   if (guide == null) {
                     guide = definition.newGuide();
                   }
                   guideComment = guide.newGuideComment();
                   guideComment.setId(Long.parseLong(content));
                 } else {
                   guideComment.setFieldValue(nodeName, content);
                 }
               } else {
                 if (parent.equals("warnings")) {
                   if (nodeName.equals("item")) {
                     if (warn == null) {
                       warn = new ArrayList<MoodleWarning>();
                     }
                     warning = new MoodleWarning();
                     warn.add(warning);
                     warning.setItem(content);
                   } else {
                     warning.setMoodleWarningField(nodeName, content);
                   }
                 }
               }
             }
           }
         }
       }
     }
   }
   if (warn != null) {
     warnings = new MoodleWarning[warn.size()];
     warnings = warn.toArray(warnings);
   }
   MoodleGradeArea[] results = null;
   if (areas != null) {
     results = new MoodleGradeArea[areas.size()];
     results = areas.toArray(results);
   }
   return results;
 }
 public static MoodleGradeInstance[] getGradingFormInstances(
     Long definitionId, Long since, MoodleWarning[] warnings)
     throws MoodleRestGradeException, UnsupportedEncodingException, MoodleRestException {
   if (MoodleCallRestWebService.isLegacy()) {
     throw new MoodleRestGradeException(MoodleRestException.NO_LEGACY);
   }
   StringBuilder data = new StringBuilder();
   String functionCall = MoodleServices.CORE_GRADING_GET_GRADINGFORM_INSTANCES.toString();
   if (MoodleCallRestWebService.getAuth() == null) {
     throw new MoodleRestGradeException();
   } else {
     data.append(MoodleCallRestWebService.getAuth());
   }
   data.append("&")
       .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
   if (definitionId == null) throw new MoodleRestGradeException("Parameter null");
   data.append("&")
       .append(URLEncoder.encode("definitionid", MoodleServices.ENCODING.toString()))
       .append("=")
       .append(URLEncoder.encode("" + definitionId, MoodleServices.ENCODING.toString()));
   if (since != null)
     data.append("&")
         .append(URLEncoder.encode("since", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(URLEncoder.encode("" + since, MoodleServices.ENCODING.toString()));
   NodeList elements = MoodleCallRestWebService.call(data.toString());
   // Process returned elements
   ArrayList<MoodleGradeInstance> instances = null;
   MoodleGradeInstance instance = null;
   MoodleGradeInstance.Guide guide = null;
   MoodleGradeInstance.Rubric rubric = null;
   MoodleGradeInstance.Criteria criteria = null;
   ArrayList<MoodleWarning> warn = null;
   MoodleWarning warning = null;
   for (int j = 0; j < elements.getLength(); j++) {
     String grandparent = null;
     String parent =
         elements
             .item(j)
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getAttributes()
             .getNamedItem("name")
             .getNodeValue();
     Node parentNode =
         elements
             .item(j)
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getParentNode()
             .getAttributes()
             .getNamedItem("name");
     Node grandparentNode = parentNode.getParentNode();
     if (grandparentNode != null) {
       grandparent = grandparentNode.getNodeValue();
     } else {
       grandparent = null;
     }
     String nodeName =
         elements.item(j).getParentNode().getAttributes().getNamedItem("name").getNodeValue();
     String content = elements.item(j).getTextContent();
     if (parent.equals("instances")) {
       if (nodeName.equals("id")) {
         if (instances == null) {
           instances = new ArrayList<MoodleGradeInstance>();
         }
         instance = new MoodleGradeInstance(Long.parseLong(content));
         instances.add(instance);
         guide = null;
         rubric = null;
       } else {
         instance.setFieldValue(nodeName, content);
       }
     } else {
       if (parent.equals("criteria") && grandparent.equals("guide")) {
         if (nodeName.equals("id")) {
           if (guide == null) guide = instance.newGuide();
           criteria = guide.newCriteria(Long.parseLong(content));
         } else {
           criteria.setFieldValue(nodeName, content);
         }
       } else {
         if (parent.equals("criteria") && grandparent.equals("rubric")) {
           if (nodeName.equals("id")) {
             if (rubric == null) rubric = instance.newRubric();
             criteria = rubric.newCriteria(Long.parseLong(content));
           } else {
             criteria.setFieldValue(nodeName, content);
           }
         } else {
           if (parent.equals("warning")) {
             if (nodeName.equals("item")) {
               if (warn == null) {
                 warn = new ArrayList<MoodleWarning>();
               }
               warning = new MoodleWarning();
               warn.add(warning);
               warning.setItem(content);
             } else {
               warning.setMoodleWarningField(nodeName, content);
             }
           }
         }
       }
     }
   }
   if (warn != null) {
     warnings = new MoodleWarning[warn.size()];
     warnings = warn.toArray(warnings);
   }
   MoodleGradeInstance[] results = null;
   if (instances != null) {
     results = new MoodleGradeInstance[instances.size()];
     results = instances.toArray(results);
   }
   return results;
 }
 /**
  * Method to send messages between the caller and other users.
  *
  * @param messages
  * @return MoodleMessage[]
  * @throws MoodleRestMessageException
  * @throws MoodleRestException
  */
 public static MoodleMessage[] sendInstantMessages(MoodleMessage[] messages)
     throws MoodleRestMessageException, MoodleRestException {
   final StringBuilder data = new StringBuilder();
   if (MoodleCallRestWebService.isLegacy()) {
     throw new MoodleRestMessageException(MoodleRestException.NO_LEGACY);
   }
   final String functionCall = MoodleServices.CORE_MESSAGE_SEND_INSTANT_MESSAGES.name();
   try {
     if (MoodleCallRestWebService.getAuth() == null) {
       throw new MoodleRestMessageException();
     } else {
       data.append(MoodleCallRestWebService.getAuth());
     }
     data.append("&")
         .append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()))
         .append("=")
         .append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));
     for (int i = 0; i < messages.length; i++) {
       if (messages[i].getToUserId() == null) {
         throw new MoodleRestMessageException(MoodleRestMessageException.NO_RECIPIENT);
       }
       if (messages[i].getText() == null) {
         throw new MoodleRestMessageException(MoodleRestMessageException.NO_MESSAGE);
       }
       data.append("&")
           .append(
               URLEncoder.encode(
                   "messages[" + i + "][touserid]", MoodleServices.ENCODING.toString()))
           .append("=")
           .append(
               URLEncoder.encode(
                   Long.toString(messages[i].getToUserId()), MoodleServices.ENCODING.toString()));
       data.append("&")
           .append(
               URLEncoder.encode("messages[" + i + "][text]", MoodleServices.ENCODING.toString()))
           .append("=")
           .append(URLEncoder.encode(messages[i].getText(), MoodleServices.ENCODING.toString()));
       if (messages[i].getClientMsgId() != null) {
         data.append("&")
             .append(
                 URLEncoder.encode(
                     "messages[" + i + "][clientmsgid]", MoodleServices.ENCODING.toString()))
             .append("=")
             .append(
                 URLEncoder.encode(
                     messages[i].getClientMsgId(), MoodleServices.ENCODING.toString()));
       }
     }
     data.trimToSize();
     final NodeList elements = MoodleCallRestWebService.call(data.toString());
     for (int j = 0, i = -1; j < elements.getLength(); j++) {
       String parent =
           elements
               .item(j)
               .getParentNode()
               .getParentNode()
               .getParentNode()
               .getParentNode()
               .getNodeName();
       if (parent.equals("KEY")) {
         parent =
             elements
                 .item(j)
                 .getParentNode()
                 .getParentNode()
                 .getParentNode()
                 .getParentNode()
                 .getAttributes()
                 .getNamedItem("name")
                 .getNodeValue();
       }
       final String content = elements.item(j).getTextContent();
       final String nodeName =
           elements.item(j).getParentNode().getAttributes().getNamedItem("name").getNodeValue();
       if (parent.equals("RESPONSE") && nodeName.equals("msgid")) {
         i++;
         messages[i].setMsgId(Long.parseLong(content));
       } else {
         messages[i].setMoodleMesageField(nodeName, content);
         if (nodeName.equals("errormessage") && content.length() != 0) {
           throw new MoodleRestMessageException(content);
         }
       }
     }
   } catch (final UnsupportedEncodingException ex) {
     Logger.getLogger(MoodleRestMessage.class.getName()).log(Level.SEVERE, null, ex);
   }
   return messages;
 }
  /**
   * Method to fetch information about the messaging contacts of the user
   *
   * @return MoodleContact[]
   * @throws MoodleRestMessageException
   * @throws MoodleRestException
   */
  public static MoodleContact[] getContacts()
      throws MoodleRestMessageException, MoodleRestException {
    String functionCall = null;
    StringBuilder data = new StringBuilder();
    MoodleContact[] contacts = null;

    if (MoodleCallRestWebService.isLegacy())
      throw new MoodleRestException(MoodleRestException.NO_LEGACY);
    else functionCall = MoodleServices.CORE_MESSAGE_GET_CONTACTS.name();

    try {
      if (MoodleCallRestWebService.getAuth() == null) throw new MoodleRestMessageException();
      else data.append(MoodleCallRestWebService.getAuth());

      data.append("&");
      data.append(URLEncoder.encode("wsfunction", MoodleServices.ENCODING.toString()));
      data.append("=");
      data.append(URLEncoder.encode(functionCall, MoodleServices.ENCODING.toString()));

      // get the contacts
      NodeList elements = MoodleCallRestWebService.call(data.toString());

      // parse the content. Each element is a "VALUE".
      Vector<MoodleContact> vector = new Vector<MoodleContact>();
      MoodleContact contact = null;

      for (int j = 0; j < elements.getLength(); j++) {

        //
        // instanciate the user if its null
        if (contact == null) contact = new MoodleContact(Long.valueOf("0"));
        //

        //
        // get current "VALUE" node
        Node node = elements.item(j);
        //

        //
        // get current node text value
        String nodeValue = node.getTextContent();
        //

        //
        // get current node "name" atribute
        String nodeName = node.getParentNode().getAttributes().getNamedItem("name").getNodeValue();
        //

        contact.setMoodleUserContactField(nodeName, nodeValue);

        if (nodeName.equalsIgnoreCase("unread")) {
          //
          // filtering "KEY"s with sons other than "VALUE"
          String parentNodeName = null;
          Node parentNode = node.getParentNode().getParentNode().getParentNode().getParentNode();

          if ((parentNode != null) && (parentNode.getNodeName().equalsIgnoreCase("KEY")))
            parentNodeName = parentNode.getAttributes().getNamedItem("name").getNodeValue();
          //

          //
          // getting the state of the relation of the contact
          MoodleContactState nodeState = null;
          for (MoodleContactState state : MoodleContactState.values()) {
            if ((parentNodeName != null) && (parentNodeName.equalsIgnoreCase(state.name()))) {
              nodeState = state;
              break;
            }
          }
          //

          contact.setMoodleUserContactField("state", nodeState);
          vector.add(contact);
          contact = null;
        }
      }

      // instaciate the array of contacts with the size of the vector
      contacts = new MoodleContact[vector.size()];

      // Get the parsed contacts to an array
      for (int i = 0; i < vector.size(); i++) contacts[i] = vector.get(i);

      // Clear vector
      vector.removeAllElements();

    } catch (UnsupportedEncodingException ex) {
      Logger.getLogger(MoodleRestMessage.class.getName()).log(Level.SEVERE, null, ex);
    }

    return contacts;
  }