Change folder style by metadata value
The example demonstrates how to change folder style icons based on metadata values.
When a document in a folder changes its status value, it causes the parent folder icon to change.
Configure sample
Create a new Metadata group
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 3.10//EN"
"http://www.openkm.com/dtd/property-groups-3.10.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 statuses.
Folder style name | Icon | Internal Id |
---|---|---|
green |
|
2 |
orange |
|
3 |
red |
|
4 |
More information about Folder style.
Each folder style internally has a unique Id. That Id is unique and automatically assigned by the application at the creation stage. In the sample, 2, 3, and 4 have been used as unique Ids, but in your application they may be different.
To find your Ids, put your cursor over the Edit folder style icon and copy the URL. You'll see something like this:
http://localhost:8080/openkm/admin/FolderStyle?action=edit&fs_id=2 where the value of the fs_id parameter is the Id you're looking for.
Create a new Automation task
Create an Automation task:
- Set the name "Change icon based on metadata".
- Choose "Set Property Group" event (set metadata fields event).
- Choose "post" on the "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 in the toolbar.
- The folder style icon has been changed.