Difference between revisions of "Unique name"

From OpenKM Documentation
Jump to: navigation, search
(Created page with 'The script generates unique document name based in metadata values each time new document is uploaded. <source lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE prope…')
 
(Example)
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
The script generates unique document name based in metadata values each time new document is uploaded.
 
The script generates unique document name based in metadata values each time new document is uploaded.
  
 +
 +
'''Description:'''
 +
* Property '''okp:data.id''' store unique autoincremental value.
 +
* Property '''okp:data.project.code''' store project code value.
 +
* Property '''okp:data.customer''' store customer code.
 +
* Property '''okp:data.description''' store document description.
 +
* When a new document is uploaded user must fill all fields except '''okp:data.id''' which is automatically set by OpenKM.
 +
* When PropertyGroup is changed - event fired - is executed automatic code which generates '''okp:data.id''' and rename document based on '''projectCode-autonumericId-clientCode-description.documentExtension'''.
 +
 +
 +
'''Property group definition:'''
 
<source lang="xml">
 
<source lang="xml">
 
<?xml version="1.0" encoding="UTF-8"?>
 
<?xml version="1.0" encoding="UTF-8"?>
Line 8: Line 19:
 
     <property-group label="Datos" name="okg:data">
 
     <property-group label="Datos" name="okg:data">
 
<input label="Id" type="text" name="okp:data.id" width="200px" readonly="true"/>
 
<input label="Id" type="text" name="okp:data.id" width="200px" readonly="true"/>
<input label="Código proyecto" type="text" name="okp:data.codigo.proyecto" width="200px">
+
<input label="Project code" type="text" name="okp:data.project.code" width="200px">
 
<validator type="req"/>
 
<validator type="req"/>
 
<validator type="num"/>
 
<validator type="num"/>
Line 14: Line 25:
 
<validator type="minlen" parameter="6"/>
 
<validator type="minlen" parameter="6"/>
 
</input>
 
</input>
<select label="Cliente" name="okp:data.cliente" type="simple"  
+
<select label="Customer" name="okp:data.customer" type="simple"  
table="clientes"  
+
table="customer"  
optionsQuery="select $cli_id, $cli_nombre from DatabaseMetadataValue dmv where dmv.table='clientes'">
+
optionsQuery="select $cus_id, $cus_nombre from DatabaseMetadataValue dmv where dmv.table='customer'">
 
<validator type="req"/>
 
<validator type="req"/>
 
</select>
 
</select>
<input label="Descripción" type="text" name="okp:data.description" width="200px">
+
<input label="Description" type="text" name="okp:data.description" width="200px">
 
<validator type="req"/>
 
<validator type="req"/>
 
<validator type="maxlen" parameter="150"/>
 
<validator type="maxlen" parameter="150"/>
Line 25: Line 36:
 
   </property-group>
 
   </property-group>
 
</property-groups>
 
</property-groups>
 +
</source>
 +
 +
 +
'''Database metadata:'''
 +
<source lang="sql">
 +
-- metadata type
 +
DELETE FROM OKM_DB_METADATA_TYPE WHERE DMT_TABLE='customer';
 +
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_TYPE, DMT_VIRTUAL_COLUMN) VALUES ('customer', 'col00', 'integer', 'cus_id');
 +
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_TYPE, DMT_VIRTUAL_COLUMN) VALUES ('customer', 'col01', 'text', 'cus_nombre');
  
 +
-- values
 +
DELETE FROM OKM_DB_METADATA_VALUE WHERE DMV_TABLE='customer';
 +
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01) VALUES ('customer', '0001','Customer 1');
 +
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01) VALUES ('customer', '0002','Customer 2');
 
</source>
 
</source>
  
 +
 +
'''Code:'''
 +
<source lang="java">
 +
import com.openkm.api.OKMPropertyGroup;
 +
import com.openkm.api.OKMRepository;
 +
import java.util.*;
 +
import com.openkm.dao.DatabaseMetadataDAO;
 +
import com.openkm.api.OKMPropertyGroup;
 +
import com.openkm.bean.form.FormElement;
 +
import com.openkm.bean.form.Input;
 +
import com.openkm.bean.form.Select;
 +
import com.openkm.bean.form.Option;
 +
import com.openkm.util.PathUtils;
 +
import com.openkm.util.FileUtils;
 +
import com.openkm.api.OKMDocument;
 +
 
 +
String grpName = "okg:data";
 +
String table = "autonumber";
 +
String sequenceName = "doc_id";
 +
String path = OKMRepository.getInstance().getNodePath(null,uuid);
 +
 
 +
// Evaluate if already has property group
 +
boolean add = true;
 +
String prjCode = "";
 +
String clientCode = "";
 +
String docId = "";
 +
String desc = "";
 +
for (FormElement formElement : OKMPropertyGroup.getInstance().getProperties(null, path, grpName)) {
 +
    if (formElement.getName().equals("okp:data.id")) {
 +
        docId = ((Input) formElement).getValue();
 +
        add = docId.equals("");
 +
    } else if (formElement.getName().equals("okp:data.project.code")) {
 +
        prjCode = ((Input) formElement).getValue();
 +
    } else if (formElement.getName().equals("okp:data.description")) {
 +
        desc = ((Input) formElement).getValue();
 +
    } else if (formElement.getName().equals("okp:data.customer")) {
 +
        for (Option option: ((Select) formElement).getOptions()) {
 +
            if (option.isSelected()) {
 +
                clientCode = option.getValue();
 +
            }
 +
        }   
 +
    }
 +
}
 +
 
 +
// Setting properties
 +
if (add) {
 +
    // add unique document id
 +
    docId = String.valueOf(DatabaseMetadataDAO.getNextSequenceValue(table, sequenceName));
 +
    switch (docId.length()) {
 +
        case 1:
 +
            docId = "0000" + docId;
 +
            break;
 +
        case 2:
 +
            docId = "000" + docId;
 +
            break;
 +
        case 3:
 +
            docId = "00" + docId;
 +
            break;
 +
        case 4:
 +
            docId = "0" + docId;
 +
            break;
 +
    }
 +
    Map map = new HashMap();
 +
    map.put("okp:data.id",docId);
 +
    OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);
 +
}
 +
 
 +
// rename document
 +
String newName = prjCode + "-" + docId + "-" + clientCode + "-" + desc + "." + FileUtils.getFileExtension(PathUtils.getName(path));
 +
OKMDocument.getInstance().rename(null, path, newName);
 +
</source>
 +
 +
== Example ==
 +
'''Register property groups:'''
 +
[[File:Okm_user_guide_440.png|center]]
 +
 +
 +
'''Create automation task to show property group in document wizard:'''
 +
[[File:Okm_user_guide_441.png|center]]
 +
 +
[[File:Okm_user_guide_442.png|center]]
 +
 +
 +
'''Create automation task based on property_group_set event:'''
 +
[[File:Okm_user_guide_445.png|center]]
 +
 +
[[File:Okm_user_guide_446.png|center]]
 +
 +
 +
'''Upload new file and fill metadata fields:'''
 +
[[File:Okm_user_guide_443.png|center]]
 +
 +
 +
'''Automatically is created document name based in metadata fields:'''
 +
[[File:Okm_user_guide_444.png|center]]
  
 
[[Category: Utilities]]
 
[[Category: Utilities]]

Latest revision as of 14:07, 2 May 2013

The script generates unique document name based in metadata values each time new document is uploaded.


Description:

  • Property okp:data.id store unique autoincremental value.
  • Property okp:data.project.code store project code value.
  • Property okp:data.customer store customer code.
  • Property okp:data.description store document description.
  • When a new document is uploaded user must fill all fields except okp:data.id which is automatically set by OpenKM.
  • When PropertyGroup is changed - event fired - is executed automatic code which generates okp:data.id and rename document based on projectCode-autonumericId-clientCode-description.documentExtension.


Property group definition:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 2.1//EN"
"http://www.openkm.com/dtd/property-groups-2.1.dtd">
<property-groups>
    <property-group label="Datos" name="okg:data">
		<input label="Id" type="text" name="okp:data.id" width="200px" readonly="true"/>
		<input label="Project code" type="text" name="okp:data.project.code" width="200px">
			<validator type="req"/>
			<validator type="num"/>
			<validator type="maxlen" parameter="6"/>
			<validator type="minlen" parameter="6"/>
		</input>
		<select label="Customer" name="okp:data.customer" type="simple" 
			table="customer" 
			optionsQuery="select $cus_id, $cus_nombre from DatabaseMetadataValue dmv where dmv.table='customer'">
			<validator type="req"/>
		</select>
		<input label="Description" type="text" name="okp:data.description" width="200px">
			<validator type="req"/>
			<validator type="maxlen" parameter="150"/>
		</input>
  </property-group>
</property-groups>


Database metadata:

-- metadata type
DELETE FROM OKM_DB_METADATA_TYPE WHERE DMT_TABLE='customer';
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_TYPE, DMT_VIRTUAL_COLUMN) VALUES ('customer', 'col00', 'integer', 'cus_id');
INSERT INTO OKM_DB_METADATA_TYPE (DMT_TABLE, DMT_REAL_COLUMN, DMT_TYPE, DMT_VIRTUAL_COLUMN) VALUES ('customer', 'col01', 'text', 'cus_nombre');

-- values
DELETE FROM OKM_DB_METADATA_VALUE WHERE DMV_TABLE='customer';
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01) VALUES ('customer', '0001','Customer 1');
INSERT INTO OKM_DB_METADATA_VALUE (DMV_TABLE, DMV_COL00, DMV_COL01) VALUES ('customer', '0002','Customer 2');


Code:

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import java.util.*;
import com.openkm.dao.DatabaseMetadataDAO;
import com.openkm.api.OKMPropertyGroup;
import com.openkm.bean.form.FormElement;
import com.openkm.bean.form.Input;
import com.openkm.bean.form.Select;
import com.openkm.bean.form.Option;
import com.openkm.util.PathUtils;
import com.openkm.util.FileUtils;
import com.openkm.api.OKMDocument;
   
String grpName = "okg:data";
String table = "autonumber";
String sequenceName = "doc_id";
String path = OKMRepository.getInstance().getNodePath(null,uuid);
  
// Evaluate if already has property group
boolean add = true;
String prjCode = "";
String clientCode = "";
String docId = "";
String desc = "";
for (FormElement formElement : OKMPropertyGroup.getInstance().getProperties(null, path, grpName)) {
    if (formElement.getName().equals("okp:data.id")) {
        docId = ((Input) formElement).getValue();
        add = docId.equals("");
    } else if (formElement.getName().equals("okp:data.project.code")) {
        prjCode = ((Input) formElement).getValue();
    } else if (formElement.getName().equals("okp:data.description")) {
        desc = ((Input) formElement).getValue();
    } else if (formElement.getName().equals("okp:data.customer")) {
        for (Option option: ((Select) formElement).getOptions()) {
            if (option.isSelected()) {
                clientCode = option.getValue();
            }
        }     
    }
}
   
// Setting properties
if (add) {
    // add unique document id
    docId = String.valueOf(DatabaseMetadataDAO.getNextSequenceValue(table, sequenceName));
    switch (docId.length()) {
        case 1:
            docId = "0000" + docId;
            break;
        case 2:
            docId = "000" + docId;
            break;
        case 3:
            docId = "00" + docId;
            break;
        case 4:
            docId = "0" + docId;
            break;
    }
    Map map = new HashMap();
    map.put("okp:data.id",docId);
    OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);
}
  
// rename document
String newName = prjCode + "-" + docId + "-" + clientCode + "-" + desc + "." + FileUtils.getFileExtension(PathUtils.getName(path));
OKMDocument.getInstance().rename(null, path, newName);

Example

Register property groups:

Okm user guide 440.png


Create automation task to show property group in document wizard:

Okm user guide 441.png
Okm user guide 442.png


Create automation task based on property_group_set event:

Okm user guide 445.png
Okm user guide 446.png


Upload new file and fill metadata fields:

Okm user guide 443.png


Automatically is created document name based in metadata fields:

Okm user guide 444.png