AnsweredAssumed Answered

ConcurrencyFailureException while uploading 3 or more documents using nodepropertyupdate

Question asked by kranthi on Mar 8, 2017

ConcurrencyFailureException while uploading 3 or more documents using nodepropertyupdate .Here in updating we are adding document id to each document but one document is missing id by getting concurencyFailureException.

 

Here is my sample code for this:

public void onNodePropertyUpdate(NodeRef nodeRef0,
Map<QName, Serializable> before0, Map<QName, Serializable> after0)// throws
// SQLException
{
final NodeRef nodeRef = nodeRef0;
final Map<QName, Serializable> before = before0;
final Map<QName, Serializable> after = after0;
AuthenticationUtil.RunAsWork task = new AuthenticationUtil.RunAsWork() {
@Override
public Object doWork() throws Exception
{

try {
if (nodeRef == null)
return 0;
if (before == null)
return 0;
if (before.isEmpty())
return 0;// create node.
logger.debug("1");
if (!exists(nodeRef))
return 0;
logger.debug("2");
if (nodeService.hasAspect(nodeRef,
ContentModel.ASPECT_WORKING_COPY))
return 0;
logger.debug("3");
if (authenticationService.isCurrentUserTheSystemUser())
return 0;
logger.debug("4");
if (after == null)
return 0;
String beforeDept = (String) before.get(RModel.PROP_DEPARTMENT);
String beforeFacility = (String) before.get(RSModel.PROP_FACILITY);
String afterDept = (String) after.get(RModel.PROP_DEPARTMENT);
String afterFacility = (String) after.get(RSModel.PROP_FACILITY);

String beforeDocType = (String) before.get(RSModel.PROP_TYPE);
String afterDocType = (String) after.get(RModel.PROP_TYPE);
// first time adding properties
logger.debug("update property called for nodeRef: "+ nodeRef);
logger.debug("beforeProps: " + beforeDept + " # "+ beforeFacility + " # " + beforeDocType);
logger.debug("afterProps: " + afterDept + " # "+ afterFacility + " # " + afterDocType);
String beforeName = (String) before.get(ContentModel.PROP_NAME);
String afterName = (String) after.get(ContentModel.PROP_NAME);

if (afterName != null && !beforeName.equals(afterName)) {
List<String> publishedNodeRefVals = (List<String>) nodeService.getProperty(nodeRef,RModel.PROP_PUBLISHED_NODEREFS);
ArrayList<String> publishedRefs = new ArrayList<String>();
if (publishedNodeRefVals != null&& publishedNodeRefVals.size() > 0) {
for (String p : publishedNodeRefVals) {
String[] pubRef = p.split("#");
NodeRef publishedRef = new NodeRef(pubRef[0]);
if(nodeService.exists(publishedRef)){
String pubName = (String) nodeService.getProperty(publishedRef,ContentModel.PROP_NAME);
int dotIndex = pubName.lastIndexOf(".");
String ext = "";
if (dotIndex != -1) {
ext = pubName.substring(dotIndex);
}
behaviourFilter.disableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
nodeService.setProperty(publishedRef,ContentModel.PROP_NAME,afterName + ext);
publishedRefs.add(publishedRef.toString()+ "#"+ afterName+ ext+ "#"+ Repository.getDisplayPath(nodeService.getPath(publishedRef)));
behaviourFilter.enableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
}
}
}

if (publishedRefs.size() > 0) {
behaviourFilter.disableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
nodeService.setProperty(nodeRef,RModel.PROP_PUBLISHED_NODEREFS,publishedRefs);
behaviourFilter.enableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
}

}

if (beforeDept == null && beforeFacility == null&& afterDept != null && afterFacility != null) {
behaviourFilter.disableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
generateDocId(nodeRef);
nodeService.setProperty(nodeRef, RModel.PROP_STATE,"Working Document");
behaviourFilter.enableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
} else if (beforeDept != null && beforeFacility != null) {
if ((afterDept != null && !afterDept.equalsIgnoreCase(beforeDept))|| (afterFacility != null && !afterFacility.equalsIgnoreCase(beforeFacility))) {
behaviourFilter.disableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
generateDocId(nodeRef);
behaviourFilter.enableBehaviour(nodeRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
List<String> publishedNodeRefVals = (List<String>) nodeService.getProperty(nodeRef,RModel.PROP_PUBLISHED_NODEREFS);

ArrayList<String> publishedRefs = new ArrayList<String>();
if (publishedNodeRefVals != null&& publishedNodeRefVals.size() > 0) {
for (String p : publishedNodeRefVals) {
String[] pubRef = p.split("#");
NodeRef publishedRef = new NodeRef(pubRef[0]);
if(nodeService.exists(publishedRef)){
behaviourFilter.disableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
nodeService.setProperty(publishedRef,RModel.PROP_DOC_ID,nodeService.getProperty(nodeRef,RModel.PROP_DOC_ID));
nodeService.setProperty(publishedRef,RModel.PROP_DEPARTMENT,afterDept);
nodeService.setProperty(publishedRef,RModel.PROP_FACILITY,afterFacility);
behaviourFilter.enableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
}
}
}
}
}

if (beforeDocType != null) {
if (afterDocType != null&& !afterDocType.equalsIgnoreCase(beforeDocType)) {
List<String> publishedNodeRefVals = (List<String>) nodeService.getProperty(nodeRef,RModel.PROP_PUBLISHED_NODEREFS);
if (publishedNodeRefVals != null&& publishedNodeRefVals.size() > 0) {
for (String p : publishedNodeRefVals) {
String[] pubRef = p.split("#");
NodeRef publishedRef = new NodeRef(pubRef[0]);
if(nodeService.exists(publishedRef)){
behaviourFilter.disableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
nodeService.setProperty(publishedRef,RModel.PROP_TYPE, afterDocType);
behaviourFilter.enableBehaviour(publishedRef,NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME);
}}}}}

}

catch (Exception e) {
logger.error("Exception e:"+e); // throw(e);
}
return 0;
}};
AuthenticationUtil.runAs(task, AuthenticationUtil.getAdminUserName());
}

 

private void generateDocId(NodeRef nodeRef) {
String query = "PATH:\"//app:company_home/st:sites/cm:file_net//*\" AND TYPE:\"r:docIdGenrationList\"";
List<NodeRef> refs = luceneSearch(query);
if (refs != null) {
NodeRef node = refs.get(0);
String docIdFormat = (String) nodeService.getProperty(node,QName.createQName("{http://www.rsystems.com/model/content/1.0}docIdFomat"));
String[] docIdFormats = docIdFormat.split("-");
String docId = "";
for (int i = 0, l = docIdFormats.length; i < l; i++) {
String p = docIdFormats[i];
if (p.charAt(0) == '<' && p.charAt(p.length() - 1) == '>') {
p = p.substring(1, p.length() - 1);
if (p.equals("date")) {
}
if (p.equals("day")) {
}
if (p.equals("month")) {
}
if (p.equals("year")) {
} else {
String val = (String) nodeService.getProperty(nodeRef,QName.createQName("{http://www.rsystems.com/model/content/1.0}"+ p));
if (val != null) {
if (docId.length() > 0)
docId += "-" + val;
else
docId = val;
} else {
if (docId.length() > 0)
docId += "-" + p;
else
docId = p;
}
}
} else {
if (p.equals("SEQ")) {
String docIdOld = (String) nodeService.getProperty(nodeRef, RModel.PROP_DOC_ID);
if (docIdOld == null || docIdOld.length() < 1) {
Integer currId = (Integer) nodeService.getProperty(node,QName.createQName("{http://www.rsystems.com/model/content/1.0}docCurrentId"));
Integer digits = (Integer) nodeService.getProperty(node,QName.createQName("{http://www.rsystems.com/model/content/1.0}numberOfDigits"));
String cId = Integer.toString(currId);
while (digits > cId.length()) {
cId = "0" + cId;
}
if (docId.length() > 0)
docId += "-" + cId;
else
docId = cId;
logger.debug("cuurId in setDocId: " + currId);
nodeService.setProperty(node,QName.createQName("{http://www.rsystems.com/model/content/1.0}docCurrentId"),currId + 1);
} else {

String[] oldIds = docIdOld.split("-");
String cId = oldIds[oldIds.length - 1];
if (docId.length() > 0)
docId += "-" + cId;
else
docId = docIdOld;
logger.debug("docId in setDocId: " + docId);
}

} else {
if (docId.length() > 0)
docId += "-" + p;
else
docId = p;
}

}
}
if (docId.length() > 0) {
nodeService.setProperty(nodeRef, RModel.PROP_DOC_ID, docId);
}

}
}

Outcomes