Alfresco can I Change properties "cmis:creationDate and cmis:lastModificationDate" Updatablity? - alfresco

Hi and thanks in advance for the help
I have a problem with insertion and update documents in alfresco, So when I set a property like "cmis:creationDate or cmis:lastModificationDate", the document is created successfully but the properties that has Updatability=ReadOnly doesn't set to the new value given it's set automatically by alfresco.
Is there any solution to set Updatibility of these properties to "ReadWrite"?
I'm using Aalfresco 5.0 and openCmis 0.13 this is my code :
public void createDocument(Folder folder) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date d = sdf.parse("21/12/2012");
String name = "myNewDocument.txt";
Map<String, Object> properties = new HashMap<String, Object>();
Calendar cal = new GregorianCalendar();
cal.setTime(d);
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document,P:cm:titled,P:cm:author");
properties.put(PropertyIds.NAME, name);
properties.put(PropertyIds.CREATION_DATE, cal);
properties.put(PropertyIds.LAST_MODIFICATION_DATE, cal);
properties.put("cm:title", "Title");
properties.put("cm:description", "Description");
properties.put("cm:author", "author");
properties.put("cmis:creationDate ", cal);
byte[] content = "Hello World!".getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = new ContentStreamImpl(name, BigInteger.valueOf(content.length), "text/plain", stream);
Document newDoc = folder.createDocument(properties, contentStream, VersioningState.MAJOR);
}

Updating the read only fields requires work on the Alfresco side. There are policies in place that prevent properties of the aspect cm:auditable from being changed.
You can update the fields in Alfresco using the NodeService API after you've disabled that policy behavior. Here's an example:
policyBehaviourFilter.disableBehaviour(node, ContentModel.ASPECT_AUDITABLE);
// Update CreatedDate
nodeService.setProperty(node, ContentModel.PROP_CREATED, dateTime);
//Enable policy
policyBehaviourFilter.enableBehaviour(node, ContentModel.ASPECT_AUDITABLE);
You could package this into a custom webscript to allow the properties to be changed remotely.

Related

Properties from aspects not set in CMIS 1.1

We have a problem with a Folder object and custom aspects:
...
properties.put(PropertyIds.OBJECT_TYPE_ID, "F:sd:folderDocument,P:sd:info");
properties.put("sd:cause", "asdfg");
Folder stDocument = folder.createFolder(properties);
...
Conten of sd:cause is "nothing" in CMIS 1.1 but in CMIS 1.0 work fine.
NOT WORK!
params.put(SessionParameter.ATOMPUB_URL, "http://localhost:8084/alfresco/api/-default-/public/cmis/versions/1.1/atom");
WORK!
params.put(SessionParameter.ATOMPUB_URL, "http://localhost:8084/alfresco/api/-default-/public/cmis/versions/1.0/atom");
We need work in version 1.1
In CMIS 1.1 you add an aspect by adding the aspect type ID to the cmis:secondaryObjectTypeIds property. Here is an example: https://gist.github.com/jpotts/7242070
Make sure you are NOT using the alfresco object factory from the CMIS extensions project when using CMIS 1.1.
The unit test with cmis:secondaryObjectTypeIds is:
#Test
public void createStDocumentWithCMIS11() {
String folderId = "workspace://SpacesStore/03de40f1-e80d-4e0d-8b67-67e93f6e30a1";
// Connection and session to CMIS 1.1
HashMap<String, String> params = new HashMap<>();
params.put(SessionParameter.ATOMPUB_URL, "http://localhost:8084/alfresco/api/-default-/cmis/versions/1.1/atom");
params.put(SessionParameter.USER, "admin");
params.put(SessionParameter.PASSWORD, "admin");
params.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
params.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
SessionFactory factory = SessionFactoryImpl.newInstance();
Session session = factory.getRepositories(params).get(0).createSession();
// Find root folder
Folder folder = (Folder) session.getObject(folderId);
assertNotNull(folder);
// Properties for type
Map<String, Object> properties = new HashMap<>();
properties.put(PropertyIds.NAME, "Test CMIS folder type stDocument");
properties.put(PropertyIds.OBJECT_TYPE_ID, "F:sd:structDocument");
properties.put("sd:situation", "situation");
// Create folder
Folder stDocument = folder.createFolder(properties);
assertNotNull(stDocument);
// Add secondary objects (Aspects)
List<Object> aspects = stDocument.getProperty("cmis:secondaryObjectTypeIds").getValues();
aspects.add("P:sd:additionalInfo");
HashMap<String, Object> props = new HashMap<>();
props.put("cmis:secondaryObjectTypeIds", aspects);
stDocument.updateProperties(props);
// Add aspect's property
HashMap<String, Object> propsAspects = new HashMap<>();
propsAspects.put("sd:cause", "test");
stDocument.updateProperties(propsAspects);
assertEquals("test", stDocument.getProperty("sd:cause").getValueAsString());
}
But not work... :(

Property 'pos:empCode' is not valid for this type or one of the secondary types! error setting custom object in cmis

When I am adding my custom object in map, it gives me error.
java.lang.IllegalArgumentException: Property 'pos:empCode' is not
valid for
this type or one of the secondary types! at org.apache.chemistry.opencmis.client.runtime.repository.ObjectFactoryImpl.conver
tProperties(ObjectFactoryImpl.java:426) at
org.apache.chemistry.opencmis.client.runtime.SessionImpl.createDocument(Session
Impl.java:1091) at org.apache.chemistry.opencmis.client.runtime.FolderImpl.createDocument(FolderImp
l.java:77)
My code is:
Map<String, Object> metaData = new HashMap<String, Object>(0);
metaData.put(DocumentConstants.EMPCODE, empCode);
// metaData.put(DocumentConstants.TYPE, Constants.EMP_FILE_UPLOAD);
// metaData.put(DocumentConstants.SUBTYPE, Constants.ADD);
docService.uploadDocumentsForAlfresco(metaData, byteArray, fileName);
DocService:
public Boolean uploadDocumentsForAlfresco(Map<String, Object> metaData,
byte[] data, String name) {
Session session = connect();
String folderPath = null;
folderPath = cmisSite.concat(cmisPath).concat("documentlibrary/");
// String path =
"DATAFILES/".concat(metaData.get(DocumentConstants.EMPCODE).toString());
String path = "DATAFILES/".concat("6");
folderPath = folderPath.concat(path);
Folder folder = createFolder(session, folderPath);
// metaData.put(PropertyIds.OBJECT_TYPE_ID, "D:ebs:bulkUploadDoc");
metaData.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
// set the alfresco object factory
metaData.put(PropertyIds.NAME, name);
ByteArrayInputStream input = new ByteArrayInputStream(data);
ContentStream contentStream =
session.getObjectFactory().createContentStream(name, data.length,
"application/octet-stream", input);
try {
folder.createDocument(metaData, contentStream,
VersioningState.MAJOR);
return true;
} catch (Exception ex) {
//log.error("exception while uploading document",ex);
ex.printStackTrace();
return false;
}
}
added dependency in pom.xml
<dependency>
<groupId>org.alfresco.cmis.client</groupId>
<artifactId>alfresco-opencmis-extension</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>org.apache.chemistry.opencmis</groupId>
<artifactId>chemistry-opencmis-client-impl</artifactId>
<version>0.13.0</version>
</dependency>
pos:empCode is a property that is defined on one of your custom types or aspects. You are attempting to set the value of that property on the object, but you've told CMIS that the object type is cmis:document. The cmis:document type maps to cm:content, the out-of-the-box type which does not have your custom property.
Instead of using cmis:document as the object type ID, use the object type ID of the type from your custom model that defines the pos:empCode property.

2sxc - Get EntityId or EntityGuid of newly created object inside custom api controller

For adding new entity inside my custom API I use code like this:
var tagGroup = new Dictionary<string, object>();
tagGroup.Add("Title", "Some new tagGroup name");
App.Data.Create("TagGroup", tagGroup, "WebApiUser");
This work fine if I just need to add new entity, but If I need to do more operations and in this opetarions I need to use Id or Guid of this newly created entity I don't know how to get it.
One way is to get this new entity by its title back, but If this title is not unique I don't know how?
I try with this code:
dynamic obj1 = App.Data.Create("TagGroup", tagGroup, "Web Api User");
// or
var obj2 = App.Data.Create("TagGroup", tagGroup, "Web Api User");
But get error becouse function don't return any value.. Is there any other way how to make this work?
Is posible to define guid of new entity in advance like for IsPublished?
====================
Solution 1:
var tGuid = System.Guid.NewGuid().ToString();
tagGroup.Add("EntityGuid", tGuid);
and work OK
I am OK with solution to set EntityGuid in custom api controller to get new EntityId
var tGuid = System.Guid.NewGuid();
var tagGroup = new Dictionary<string, object>();
tagGroup.Add("Title", "Some new tagGroup name");
tagGroup.Add("EntityGuid", tGuid);
App.Data.Create("TagGroup", tagGroup, "WebApiUser");
var tNewObj =AsDynamic(App.Data["TagGroup"]).Where(x=>x.EntityGuid==tGuid).First();
var tNewEntityId = tNewObj.EntityId;
// do whatewer you want with this entity..
This is my final code...
Update: 2sxc now has this functionality. .Create() now returns the newly created Entity...
In 2sxc, after an App.Data.Create(), is there a way to get the new EntityId (or Guid) or a Pointer to the new Entity?

How to access document on alfresco?

I created a project alfresco amp.
To add a document, I run this Test class:
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException {
Map<String, String> sessionParameters = new HashMap<String, String>();
sessionParameters.put(SessionParameter.USER, "admin");
sessionParameters.put(SessionParameter.PASSWORD, "admin");
sessionParameters.put(SessionParameter.ATOMPUB_URL, "http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom");
sessionParameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Session lSession = sessionFactory.getRepositories(sessionParameters).get(0).createSession();
Folder root = lSession.getRootFolder();
Map<String, Object> folderProperties = new HashMap<String, Object>();
folderProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
folderProperties.put(PropertyIds.NAME, "oo");
Folder newFolder = root.createFolder(folderProperties);
Map<String, Object> lProperties = new HashMap<String, Object>();
String name = "lol.txt";
lProperties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
lProperties.put(PropertyIds.NAME, name);
byte[] content = "CMIS Testdata One".getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = new ContentStreamImpl(name, new BigInteger(content), "text/plain", stream);
Document newContent1 = newFolder.createDocument(lProperties, contentStream, null);
System.out.println("Document created: " + newContent1.getId());
}
}
The document is created with success; I got: Document created: e3184105-e59e-4b8a-88e7-9442942433a4;1.0
My problem is how can I access to this document (With which url can I access to that document).
Please help?.
It looks like you've created a document and you now want to know what URL to use to get to it. You have many options, some of which include...
Use the Alfresco web app's download URL:
http://localhost:8080/alfresco/s/api/node/workspace/SpacesStore/dac36aab-dd49-4abc-a4bc-0e0d5729c9ad/content;cm%3Acontent
Use the Share web app's download URL:
http://localhost:8080/share/proxy/alfresco/slingshot/node/content/workspace/SpacesStore/dac36aab-dd49-4abc-a4bc-0e0d5729c9ad/test.txt
Use the CMIS URL (AtomPub binding):
http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/content/test.txt?id=dac36aab-dd49-4abc-a4bc-0e0d5729c9ad%3B1.0
Use the CMIS URL (Browser binding):
http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser/root?objectId=dac36aab-dd49-4abc-a4bc-0e0d5729c9ad%3B1.0&cmisselector=content
Write your own URL handler that fetches the input stream via CMIS and returns that stream to the requester. Assuming you are using something like Spring MVC, the code for that might look like:
public InputStream download(String objectId) {
Session session = getSession();
CmisObject obj = session.getObject(objectId);
Document doc = null;
if (obj.getBaseTypeId().equals(BaseTypeId.CMIS_DOCUMENT)) {
doc = (Document) obj;
}
return doc.getContentStream().getStream();
}
Each of the above options assumes a test file in a test folder named "test.txt" with an Alfresco Node Reference of:
workspace://SpacesStore/dac36aab-dd49-4abc-a4bc-0e0d5729c9ad
And a CMIS Object ID of:
dac36aab-dd49-4abc-a4bc-0e0d5729c9ad;1.0

How to configure custom aspects and retrieve its property using CMIS 1.1 atom binding alfresco 5.0.c?

Using Alfresco, I have created an aspect and added custom properties to it.
Need help in accessing the custom properties using CMIS 1.1 Atom Binding and Alfresco 5.0.c.
Kindly help me to display the document with its custom properties.
public void uploadDocument(Folder newFolder) {
File file = new File("file.txt");
properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, file.getName());
properties.put(PropertyIds.CUSTOM_FIELD_NAME,file.getCustomField());
path = Paths.get(DOC_NAME_WITH_PATH);
content= Files.readAllBytes(path);
contentStream = new ContentStreamImpl(file.getName(),
BigInteger.valueOf(content.length), new
MimetypesFileTypeMap().getContentType(file), new
FileInputStream(file));
newDoc = (Document)newFolder.createDocument(properties, contentStream,
VersioningState.MAJOR);
}

Resources