05-02-2011 11:06 AM
05-06-2011 03:51 AM
package com.behaviour;
import java.io.Serializable;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.node.NodeServicePolicies.OnMoveNodePolicy;
import org.alfresco.repo.node.NodeServicePolicies.OnUpdatePropertiesPolicy;
import org.alfresco.repo.policy.Behaviour;
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
public class NameQNameSynchBehaviour implements OnUpdatePropertiesPolicy, OnMoveNodePolicy
{
private Logger logger = Logger.getLogger(PolicyLogger.class);
// Behaviours
private Behaviour onUpdateProperties;
private Behaviour onMoveNode;
private NodeService nodeService;
// Dependencies
private PolicyComponent policyComponent;
PocService pocService;
void init()
{
pocService = new PocService();
pocService.setNodeService(nodeService);
if (logger.isDebugEnabled()) logger.debug("Initializing policy logger behaviour");
// Create behaviours
this.onUpdateProperties = new JavaBehaviour(this, "onUpdateProperties", NotificationFrequency.EVERY_EVENT);
this.onMoveNode = new JavaBehaviour(this, "onMoveNode", NotificationFrequency.EVERY_EVENT);
// // Bind behaviours to node policies
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateProperties"), ContentModel.TYPE_CATEGORY, this.onUpdateProperties);
this.policyComponent.bindClassBehaviour(QName.createQName (NamespaceService.ALFRESCO_URI, "onMoveNode"), ContentModel.TYPE_CATEGORY, this.onMoveNode);
}
public void onUpdateProperties(NodeRef nodeRef, Map<QName, Serializable> before,Map<QName, Serializable> after)
{
logger.debug("onUpdateProperties: " + nodeRef);
// name changes
String nameBefore = (String) before.get(ContentModel.PROP_NAME);
String nameAfter = (String) after.get(ContentModel.PROP_NAME);
boolean nameChanged = (nameBefore != null && nameBefore != nameAfter);
if (nameChanged==true) {
logger.debug("Poc name has changed – nameBefore: $nameBefore, nameAfter: $nameAfter");
ChildAssociationRef newChildRef = pocService.changePrimaryParent2ChildAssocQName(nodeRef, nameAfter);
logger.debug("Poc qname has been synched: ${newChildRef.getQName()}");
}
}
public void onMoveNode(ChildAssociationRef oldChildAssocRef, ChildAssociationRef newChildAssocRef)
{
logger.debug("onMoveNode - OLD: " + oldChildAssocRef + " - NEW: " + newChildAssocRef);
// track qname changes (we can't do any more here)
QName qnameBefore = oldChildAssocRef.getQName();
QName qnameAfter = newChildAssocRef.getQName();
boolean qnameChanged = (!qnameBefore.equals(qnameAfter));
if (qnameChanged==true) {
logger.debug("Poc qname has changed – qnameBefore: $qnameBefore, qnameAfter: $qnameAfter");
// FIXME we can't backward-synch the cm:name, cause the qname may be a cutted version of a longer name
}
}
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
}
package com.behaviour;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;
public class PocService {
NodeService nodeService;
// NodeService nodeService = serviceRegistry.getNodeService();
private Logger logger = Logger.getLogger(PolicyLogger.class);
public QName createNewChildAssocQName(ChildAssociationRef childAssoc, String newNameWithExt) {
String localName = QName.createValidLocalName(newNameWithExt); // name will be croped to a maximum of 100 chars
return QName.createQName(childAssoc.getQName().getNamespaceURI(), localName);
}
public ChildAssociationRef changePrimaryParent2ChildAssocQName(NodeRef node, String newNameWithExt) {
return changeChildAssocQName(
nodeService.getPrimaryParent(
node),
newNameWithExt);
}
public ChildAssociationRef changeChildAssocQName(ChildAssociationRef childAssoc, String newNameWithExt) {
QName newAssocQName = createNewChildAssocQName(childAssoc, newNameWithExt);
if (newAssocQName.equals(childAssoc.getQName())) {
return childAssoc; // qname has not changed!
}
return nodeService.moveNode(
childAssoc.getChildRef(), childAssoc.getParentRef(),
childAssoc.getTypeQName(), newAssocQName);
}
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
}
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean id="nameQNameSynchBehaviour" class="com.behaviour.NameQNameSynchBehaviour" init-method="init">
<property name="policyComponent">
<ref bean="policyComponent" />
</property>
<property name="nodeService">
<ref bean="nodeService" />
</property>
</bean>
</beans>
05-06-2011 04:08 AM
05-06-2011 04:24 AM
05-06-2011 04:58 AM
05-06-2011 05:08 AM
05-06-2011 06:08 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.