final IStylesheetDescriptor stylesheetDescriptor = this.stylesheetDescriptorDao
.getStylesheetDescriptor(structureStylesheetId);
final List<org.dom4j.Element> structureAttributes = layout.selectNodes("//" + nodeType + "-attribute");
IStylesheetUserPreferences ssup = this.stylesheetUserPreferencesDao
.getStylesheetUserPreferences(stylesheetDescriptor, person, profile);
if (structureAttributes.isEmpty()) {
if (ssup != null) {
this.stylesheetUserPreferencesDao.deleteStylesheetUserPreferences(ssup);
}
}
else {
if (ssup == null) {
ssup = this.stylesheetUserPreferencesDao.createStylesheetUserPreferences(stylesheetDescriptor,
person,
profile);
}
final Map<String, Map<String, String>> oldLayoutAttributes = new HashMap<String, Map<String,String>>();
for (final String nodeId : ssup.getAllLayoutAttributeNodeIds()) {
final MapPopulator<String, String> nodeAttributes = new MapPopulator<String, String>();
ssup.populateLayoutAttributes(nodeId, nodeAttributes);
oldLayoutAttributes.put(nodeId, nodeAttributes.getMap());
}
for (final org.dom4j.Element structureAttribute : structureAttributes) {
final org.dom4j.Element layoutElement = structureAttribute.getParent();
final String nodeId = layoutElement.valueOf("@ID");
if (StringUtils.isEmpty(nodeId)) {
logger.warn("@ID is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
}
final String name = structureAttribute.valueOf("name");
if (StringUtils.isEmpty(nodeId)) {
logger.warn("name is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
continue;
}
final String value = structureAttribute.valueOf("value");
if (StringUtils.isEmpty(nodeId)) {
logger.warn("value is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
continue;
}
//Remove from the old attrs set as we've updated it
final Map<String, String> oldAttrs = oldLayoutAttributes.get(nodeId);
if (oldAttrs != null) {
oldAttrs.remove(name);
}
ssup.setLayoutAttribute(nodeId, name, value);
// Remove the layout attribute element or DLM fails
layoutElement.remove(structureAttribute);
}
//Purge orphaned entries
for (final Entry<String, Map<String, String>> oldAttributeEntry : oldLayoutAttributes.entrySet()) {
final String nodeId = oldAttributeEntry.getKey();
for (final String name : oldAttributeEntry.getValue().keySet()) {
ssup.removeLayoutAttribute(nodeId, name);
}
}
this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(ssup);
}