/**
   * Prepara las propiedades para un documento o un directorio nuevo en Alfresco Propiedades para un
   * documento:<br>
   * - name<br>
   * - description<br>
   * - author<br>
   * - title<br>
   * Propiedades para un directorio:<br>
   * - name<br>
   * - description<br>
   * - title<br>
   *
   * @param properties HashMap con las propiedades del documento. La propiedad "name" es obligatoria
   * @param isDocument Indica si las propiedades pertenecen a un documento. Si es falso pertenecen a
   *     un directorio
   * @return NamedValue con las propiedades del documento o directorio
   */
  public static NamedValue[] prepareProperties(
      HashMap<String, String> properties, boolean isDocument) {
    // name is mandatory
    if (properties.containsKey("name") && properties.get("name") != "") {
      int i = 1;
      if (properties.containsKey("description")) i++;
      if (properties.containsKey("title")) i++;
      if (isDocument) {
        if (properties.containsKey("author")) i++;
      }
      NamedValue[] preparedProperties = new NamedValue[i];
      i = 0;

      preparedProperties[i] =
          Utils.createNamedValue(ContentModel.PROP_NAME.toString(), properties.get("name"));
      i++;
      if (properties.containsKey("description")) {
        preparedProperties[i] =
            Utils.createNamedValue(
                ContentModel.PROP_DESCRIPTION.toString(), properties.get("description"));
        i++;
      }

      if (properties.containsKey("title")) {
        preparedProperties[i] =
            Utils.createNamedValue(ContentModel.PROP_TITLE.toString(), properties.get("title"));
        i++;
      }

      if (isDocument) {
        if (properties.containsKey("author")) {
          preparedProperties[i] =
              Utils.createNamedValue(
                  ContentModel.PROP_AUTHOR.toPrefixString(), properties.get("author"));
          i++;
        }
      }
      return preparedProperties;
    } else {
      LOGGER.severe("The name propertie is mandatory");
    }
    return null;
  }
  /**
   * {@inheritDoc}
   *
   * @see org.prowim.dms.alfresco.ContentService#createNewVersion(java.lang.String,
   *     java.lang.String, java.lang.String)
   */
  @Interceptors(AuthenticationInterceptor.class)
  public VersionResult createNewVersion(String name, String username, String uuid)
      throws OntologyErrorException, DMSException {
    // get the user
    String userID = organizationEntity.getUser(username).getID();

    AuthoringServiceSoapBindingStub authoringService = WebServiceFactory.getAuthoringService();
    final Reference reference = new Reference(DMSStoreRegistry.STORE_REF, uuid, null);
    final Predicate predicate = new Predicate(new Reference[] {reference}, null, null);
    NamedValue[] comments =
        new NamedValue[] {
          Utils.createNamedValue("description", "User description"),
          Utils.createNamedValue("versionType", "MINOR"),
          Utils.createNamedValue(DMSConstants.Content.AUTHOR_PROP, userID)
        };
    VersionResult vr;
    try {
      vr = authoringService.createVersion(predicate, comments, true);
      String descriptionNew = "This is a sample description for " + name;
      Predicate pred = new Predicate(vr.getNodes(), null, null);

      NamedValue[] titledProps = new NamedValue[2];
      titledProps[0] = Utils.createNamedValue(Constants.PROP_NAME, name);
      titledProps[1] = Utils.createNamedValue(Constants.PROP_DESCRIPTION, descriptionNew);

      CMLUpdate update = new CMLUpdate(titledProps, pred, null);
      CML cml = new CML();
      cml.setUpdate(new CMLUpdate[] {update});

      WebServiceFactory.getRepositoryService().update(cml);

      return vr;
    } catch (AuthoringFault e) {
      LOG.error(
          "Could not create new version in DMS for user " + username + " and UUID " + uuid, e);
    } catch (RemoteException e) {
      LOG.error(
          "Could not create new version in DMS for user " + username + " and UUID " + uuid, e);
    }

    return null;
  }
  /**
   * Helper method to make apply the versionable aspect to a given reference
   *
   * <p>
   *
   * @param reference the reference
   * @throws Exception can occurs.
   */
  private void makeVersionable(Reference reference) throws RemoteException {
    Validate.notNull(reference);
    /** 1. the add aspect query object. */
    NamedValue[] comments =
        new NamedValue[] {
          Utils.createNamedValue("versionType", "MINOR"),
          Utils.createNamedValue("autoVersion", "false")
        };
    // MAJOR
    Predicate predicate = new Predicate(new Reference[] {reference}, null, null);
    CMLAddAspect addAspect =
        new CMLAddAspect(
            Constants.ASPECT_VERSIONABLE, comments, predicate, null); // ASPECT_VERSIONABLE

    /** 2. Create the content management language query. */
    CML cml = new CML();
    cml.setAddAspect(new CMLAddAspect[] {addAspect});

    /** 3. Execute the query, which will add the versionable aspect to the node is question. */
    this.getRepositoryService().update(cml);
  }
  /**
   * Tests the createVersion service method
   *
   * @throws Exception
   */
  public void testVersionMethods() throws Exception {
    Reference reference = createContentAtRoot("create_version_test.txt", INITIAL_VERSION_CONTENT);
    Predicate predicate = convertToPredicate(reference);

    // Get the version history (before its been versioned)
    VersionHistory emptyVersionHistory = this.authoringService.getVersionHistory(reference);
    assertNotNull(emptyVersionHistory);
    assertNull(emptyVersionHistory.getVersions());

    // Create the version
    VersionResult result =
        this.authoringService.createVersion(predicate, getVersionComments(), false);
    assertNotNull(result);
    assertEquals(1, result.getNodes().length);
    assertEquals(1, result.getVersions().length);
    Version version = result.getVersions()[0];
    assertEquals("1.0", version.getLabel());
    // TODO check commentaries
    // TODO check creator

    // Get the version history
    this.authoringService.createVersion(predicate, getVersionComments(), false);
    VersionHistory versionHistory = this.authoringService.getVersionHistory(reference);
    assertNotNull(versionHistory);
    assertEquals(2, versionHistory.getVersions().length);
    // TODO some more tests ...

    // Update the content
    this.contentService.write(
        reference, Constants.PROP_CONTENT, SECOND_VERSION_CONTENT.getBytes(), null);

    // Create another version
    VersionResult versionResult2 =
        this.authoringService.createVersion(predicate, getVersionComments(), false);
    assertNotNull(versionResult2);
    assertEquals(1, versionResult2.getNodes().length);
    assertEquals(1, versionResult2.getVersions().length);
    Version version2 = versionResult2.getVersions()[0];
    assertEquals("1.3", version2.getLabel());
    // TODO check commentaries
    // TODO check creator

    // Check the version history
    VersionHistory versionHistory2 = this.authoringService.getVersionHistory(reference);
    assertNotNull(versionHistory2);
    assertEquals(4, versionHistory2.getVersions().length);
    // TODO some more tests ...

    // Create a major version
    NamedValue versionVal = Utils.createNamedValue("versionType", "MAJOR");
    NamedValue descriptionVal = Utils.createNamedValue("description", "new description");
    NamedValue[] comments = new NamedValue[] {versionVal, descriptionVal};
    VersionResult result5 = this.authoringService.createVersion(predicate, comments, false);
    assertNotNull(result5);
    assertEquals(1, result5.getNodes().length);
    assertEquals(1, result5.getVersions().length);
    Version version5 = result5.getVersions()[0];
    assertEquals("2.0", version5.getLabel());

    // Confirm the current content of the node
    Content[] contents =
        this.contentService.read(
            new Predicate(new Reference[] {reference}, BaseWebServiceSystemTest.store, null),
            Constants.PROP_CONTENT.toString());
    Content readResult1 = contents[0];
    String content1 = ContentUtils.getContentAsString(readResult1);
    assertEquals(SECOND_VERSION_CONTENT, content1);

    // Revert the node to the first version
    this.authoringService.revertVersion(reference, "1.0");

    // Confirm that the state of the node has been reverted
    Content[] contents2 =
        this.contentService.read(
            new Predicate(new Reference[] {reference}, BaseWebServiceSystemTest.store, null),
            Constants.PROP_CONTENT.toString());
    Content readResult2 = contents2[0];
    String content2 = ContentUtils.getContentAsString(readResult2);
    assertEquals(INITIAL_VERSION_CONTENT, content2);

    // Now delete the version history
    VersionHistory deletedVersionHistory = this.authoringService.deleteAllVersions(reference);
    assertNotNull(deletedVersionHistory);
    assertNull(deletedVersionHistory.getVersions());

    // Check the version history
    VersionHistory versionHistory3 = this.authoringService.getVersionHistory(reference);
    assertNotNull(versionHistory3);
    assertNull(versionHistory3.getVersions());
  }