Change folder style by metadata value

The example demostrates how to change folder style icons based on metadata values.

When a document into a folder changes their status value it cause the change of the parent folder icon.

Configure sample

Create a new Metadata group

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 2.5//EN"
                                 "http://www.openkm.com/dtd/property-groups-2.5.dtd">
<property-groups>
	<property-group label="Invoice status" name="okg:invoicedata">
		<select label="Status" name="okp:invoicedata.status" type="simple">
    		<option label="Open" value="open" />
    		<option label="Pending to review" value="review" />
    		<option label="Closed" value="closed" />
		</select>
	</property-group>
</property-groups>

More information about Metadata.

Create new Folder styles

Create three new folder styles. Each folder style will use only a single icon for all the status.

Folder style nameIconInternal Id
green

2

orange

3

red

4

More information about Folder style.

Each folder style internaly have and unique Id. That Id is unique an automatically assigned by application on creation stage. On the sample has been used 2,3,4 unique Id but on your application can be others.

To know what are your Id put your cursor over the Edit folder style icon and copy the url. You'll see something like it:

http://localhost:8080/openkm/admin/FolderStyle?action=edit&fs_id=2 where the value of fs_id parameter is the value of the Id you're looking for.

Create a new Automation task

Create an Automation task:

  • Set name "Change icon based on metadata".
  • Choose "Set Property Group" event ( set metadata fields event ).
  • Choose "post" on at field.
  • Check Active.

  • Add an Action based on scripting and set the code below.

More information about Automation.

Scripting code

import java.util.Map;

import com.google.gson.Gson;
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMFolder;
import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import com.openkm.bean.Document;
import com.openkm.bean.PropertyGroup;
import com.openkm.util.ContextWrapper;
import com.openkm.util.PathUtils;

try {
    OKMFolder okmFolder = ContextWrapper.getContext().getBean(OKMFolder.class);
    OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
    OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class);
    OKMPropertyGroup okmPropertyGroup = ContextWrapper.getContext().getBean(OKMPropertyGroup.class);
    PathUtils pathUtils = ContextWrapper.getContext().getBean(PathUtils.class);
    Gson gson = new Gson();
    
    String grpName = "okg:invoicedata";
    boolean hasGroup = false;

    for (PropertyGroup pg : okmPropertyGroup.getGroups(null, uuid)) {
        if (pg.getName().equals(grpName)) {
            hasGroup = true;
        }
    }

    if (hasGroup) {
        Map properties = okmPropertyGroup.getProperties(null, uuid, grpName);
        for (String key : properties.keySet()) {
            String[] values = gson.fromJson(properties.get(key), String[].class);
            String value = values[0];                        
            Document document = okmDocument.getProperties(null, uuid);            
            String fldUuid = okmRepository.getNodeUuid(null, pathUtils.getParent(document.getPath()));
            if (value.equals("open")) {
                okmFolder.setStyle(null, fldUuid, 2);
            } else if (value.equals("review")) {
                okmFolder.setStyle(null, fldUuid, 3);
            } else if (value.equals("closed")) {
                okmFolder.setStyle(null, fldUuid, 4);
            }
        }
    }
} catch (Exception e) {
    System.out.println(e.getMessage());
}

 

 Check configuration

  • Update the metadata group of a document.

  • Change the metadata field value from Pending to review to closed.

  • Click on  Refresh icon at toolbar.
  • The folder style icon applyed has been changed.