Version number sample

From OpenKM Documentation
Jump to: navigation, search

This sample has been shared by OpenKM community user. More information at public forum.

The example covers the case:

  • Major version number is a character [A-Z]
  • Minor version numner is a numeric [0-100]
package com.openkm.vernum;
 
import org.hibernate.Query;
import org.hibernate.Session;
import com.openkm.dao.bean.NodeDocument;
import com.openkm.dao.bean.NodeDocumentVersion; 
 
public class alfanumerico implements VersionNumerationAdapter
{
  public String getInitialVersionNumber()
  {
    return "A";
  }
 
  public String getNextVersionNumber(Session paramSession, NodeDocument paramNodeDocument, NodeDocumentVersion paramNodeDocumentVersion, int paramInt)
  {
 
	  if(paramInt == 0  ){
 
		  String construccion = paramNodeDocumentVersion.getName();
		  String [] versionNumeros={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27"
				  ,"28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57"
				  ,"58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87"
				  ,"88","89","90","91","92","93","94","95","96","97","98","99","100"};
 
		  for (int i = 0 ; i < versionNumeros.length ; i++) {
 
			  if(construccion.equals(versionNumeros[i])){
				  	i++;
			  		construccion=versionNumeros[i];
			  		return construccion;
			  	}	
 
		  }
 
	    String str1 = paramNodeDocumentVersion.getName();
	    String str2 = "";
	    String[] arrayOfString = new String[702];
	    Query localQuery = paramSession.createQuery("from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.name=:name");
	    NodeDocumentVersion localNodeDocumentVersion = null;
	    buildR(arrayOfString, 702);
	    str2 = searchArr(arrayOfString, str1);
	    do
	    {
	      localQuery.setString("parent", paramNodeDocument.getUuid());
	      localQuery.setString("name", str2);
	      localNodeDocumentVersion = (NodeDocumentVersion)localQuery.setMaxResults(1).uniqueResult();
	    }while (localNodeDocumentVersion != null);
 
	    return str2;
  }else{
	  String construccion = paramNodeDocumentVersion.getName();
	  String construccion2=versionCons(construccion);
 
		  return construccion2;
  }	  
 
  }
 
 
  public String versionCons(String construccion){
	  String [] versionLetras={"B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	  String [] versionNumeros={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27"
			  ,"28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57"
			  ,"58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87"
			  ,"88","89","90","91","92","93","94","95","96","97","98","99","100"};
 
	  for (int i = 0 ; i < versionNumeros.length ; i++) {
 
		  if(construccion.equals(versionNumeros[i])){
			  	i++;
		  		construccion=versionNumeros[i];
 
		  	}	
 
	  }
 
	  for (int i = 0 ; i < versionLetras.length ; i++) {
 
		  if(construccion.equals(versionLetras[i])){
		  		construccion="1";
		  	}	
 
	  }
	  return construccion;
 }
 
 
 
  public void buildR(String[] paramArrayOfString, int paramInt)
  {
    char[] arrayOfChar1 = new char[1];
    char[] arrayOfChar2 = new char[2];
    for (int i = 0; i < paramInt; i++) {
      int k = i / 26;
      if (k == 0) {
        int j = i + 65;
        arrayOfChar1[0] = ((char)j);
        paramArrayOfString[i] = new String(arrayOfChar1);
      } else {
        arrayOfChar2[0] = ((char)(k + 64));
        arrayOfChar2[1] = ((char)(i % 26 + 65));
        paramArrayOfString[i] = new String(arrayOfChar2);
      }
    }
  }
 
  public String searchArr(String[] paramArrayOfString, String paramString) {
    String str = "";
    for (int i = 0; i < paramArrayOfString.length; i++) {
      if (paramArrayOfString[i].equals(paramString))
        str = paramArrayOfString[(++i)];
    }
    return str;
  }
}