I found that the line is very slow final UpdateResult[] updateResults = repositoryServiceSoapBindingStub.update(cml);It takes at first call 13 seconds and subsequentes call takes 5 seconds.I put MYSQL logging query and no slow query founded. Also, I indexed the repository. No solution give to me good result. Is it normal to take always between 5 - 13 seconds while update document? How can I speed my code?Notes: The document created contains Version Label: 1589. If first tried search forum and no solution founded. Maybe consider this https://forums.alfresco.com/en/viewtopic.php?f=53&t=2056 as same!Found below the code used. Please help me.import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.alfresco.webservice.repository.RepositoryFault;
import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.CMLUpdate;
import org.alfresco.webservice.types.CMLWriteContent;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.Node;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.ISO9075;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;
import org.apache.commons.lang.StringUtils;
public class UpdateAlfresco {
private static final Pattern PATTERN = Pattern.compile("\\{([A-Za-z_]+)\\}");
private static final String name = "http://www.alfresco.org/model/content/1.0}name";
private static final String version = "http://www.alfresco.org/model/content/1.0}versionLabel";
private static final String modified = "http://www.alfresco.org/model/content/1.0}modified";
private static final String ticketServiceAddress = "http://MACHINE:8280/alfresco/service/api/login/ticket";
private static final String endPointAddress = "http://MACHINE:8280/alfresco/api";
private static final String user = "USER";
private static final String password = "PWD";
private static final String storeScheme = "workspace";
private static final String storeAddress = "spacesStore";
private static final String rootPath = "/app:company_home/cm:IFE";
private static final String modelTypeName = "imp:base";
private static final String modelNameSpace = "http://ola.com/model/content/1.0";
private static final Store store = new Store(storeScheme, storeAddress);
/**
* @param args
*/
public static void main(String[] args) throws Exception {
WebServiceFactory.setEndpointAddress(endPointAddress);
AuthenticationUtils.startSession(user, password);
String ticket = AuthenticationUtils.getTicket();
Map<String, String> filteredMetadataProps = new HashMap<String, String>();
filteredMetadataProps.put("tipo_documento", "my_document");
filteredMetadataProps.put("entity", "182178");
InputStream dataStream = new FileInputStream(new File("C:\\alfresco\\MyDoc.pdf"));
createDocument("/cm:Entities/cm:{entity}/cm:EIE", "my_document", "application/pdf", filteredMetadataProps, dataStream);
}
public static void createDocument(String path, String type, String contentType, Map<String, String> metadata, InputStream dataStream) throws Exception {
// CML node identifier.
String id = "bbff85d8-9980-4a30-9ab9-0f80e7cc0cad";
// Translate path.
String beforeEncodePath = rootPath + translate(path, new HashMap<String, String>(metadata));
String concretePath = DocumentBrokerUtils.encodePath(beforeEncodePath);
final String documentName = DocumentBrokerUtils.getNodeName(concretePath);
// Create properties.
final List<NamedValue> contentProperties = DocumentBrokerUtils.createProperties(metadata, modelNameSpace);
contentProperties.add(Utils.createNamedValue(Constants.PROP_NAME, ISO9075.decode(documentName)));
final CML cml = new CML();
final CMLWriteContent writeContent = new CMLWriteContent();
// Verify if document already exists.
Node node = getNodeFromRepository(new Reference(store, null, concretePath));
System.out.println(MessageFormat.format("Overriding existing document: {0}", concretePath));
Reference reference = new Reference(store, id, concretePath);
Node nodeId = getNodeFromRepository(reference);
if (nodeId != null) {
System.out.println("Using NodeId");
final Predicate predicate = new Predicate(new Reference[] { reference }, store, null);
final CMLUpdate update = new CMLUpdate(contentProperties.toArray(new NamedValue[0]), predicate, id);
cml.setUpdate(new CMLUpdate[] { update });
writeContent.setWhere(predicate);
} else {
final Predicate predicate = new Predicate(new Reference[] { node.getReference() }, store, null);
final CMLUpdate update = new CMLUpdate(contentProperties.toArray(new NamedValue[0]), predicate, null);
cml.setUpdate(new CMLUpdate[] { update });
writeContent.setWhere(predicate);
}
// Write the document content.
writeContent.setProperty(Constants.PROP_CONTENT);
writeContent.setFormat(new ContentFormat(contentType, "UTF-8"));
writeContent.setContent(DocumentBrokerUtils.readFile(dataStream));
cml.setWriteContent(new CMLWriteContent[] { writeContent });
RepositoryServiceSoapBindingStub repositoryServiceSoapBindingStub = WebServiceFactory.getRepositoryService();
long ini = System.currentTimeMillis();
final UpdateResult[] updateResults = repositoryServiceSoapBindingStub.update(cml);
System.out.println("update in " + (System.currentTimeMillis() - ini) + " ms. UUID: " + updateResults[0].getDestination().getUuid());
}
private static String translate(final String abstractPath, final Map<String, String> parameters) {
String res = abstractPath;
final Matcher matcher = PATTERN.matcher(abstractPath);
while (matcher.find()) {
final String var = matcher.group(0);
final String metaKey = matcher.group(1);
final String replacement = parameters.get(metaKey);
if (replacement != null) {
res = StringUtils.replace(res, var, replacement);
} else {
System.out.println("throw new PathTranslationException");
}
}
System.out.println("translate " + res);
return res;
}
private static void buildAndValidateFullPath(final String path) throws Exception {
final String parentPath = DocumentBrokerUtils.getParentPath(path);
if (parentPath == null) {
System.out.println("throw new InvalidPathException()");
} else {
try {
// Get the current path.
WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[] { new Reference(store, null, parentPath) }, store, null));
} catch (final Exception e) {
buildAndValidateFullPath(parentPath);
createFolder(DocumentBrokerUtils.getParentPath(parentPath), DocumentBrokerUtils.getNodeName(parentPath));
}
}
}
private static Reference createFolder(final String path, final String folderName) throws Exception {
// Get Parent Reference.
final ParentReference parentReference = new ParentReference(store, null, path, Constants.ASSOC_CONTAINS, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL,
ISO9075.decode(folderName)));
final NamedValue[] contentProps = new NamedValue[1];
contentProps[0] = Utils.createNamedValue(Constants.PROP_NAME, ISO9075.decode(folderName));
final CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_FOLDER, contentProps);
final CML cml = new CML();
cml.setCreate(new CMLCreate[] { create });
final UpdateResult[] updateResult = WebServiceFactory.getRepositoryService().update(cml);
return updateResult[0].getDestination();
}
private static Node getNodeFromRepository(final Reference reference) {
Node node = null;
try {
final Node[] nodes = WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[] { reference }, store, null));
if (nodes.length > 0) {
node = nodes[0];
}
} catch (final Exception e) {
// do nothing, document does not exists.
}
return node;
}
private static Map<String, String> getProperties(final Store store, final Reference reference) throws RepositoryFault, RemoteException {
final Map<String, String> properties = new HashMap<String, String>();
final Node[] nodes = WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[] { reference }, store, null));
for (final NamedValue namedValue : nodes[0].getProperties()) {
properties.put(namedValue.getName(), namedValue.getValue());
}
return properties;
}
}