<resource-ref>
<res-ref-name>EcmService</res-ref-name>
<res-type>com.sap.ecm.api.EcmService</res-type>
</resource-ref>
private Session getSession() {
String uniqueName = "<<Any Name>>";
String secretKey = "<<Secret Key - provide any key>>";
Session openCmisSession = null;
EcmService ecmSvc = null;
InitialContext ctx;
try {
ctx = new InitialContext();
String lookupName = "java:comp/env/" + "EcmService";
ecmSvc = (EcmService) ctx.lookup(lookupName);
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// connect to my repository
openCmisSession = ecmSvc.connect(uniqueName, secretKey);
} catch (CmisObjectNotFoundException e) {
// repository does not exist, so try to create it
RepositoryOptions options = new RepositoryOptions();
options.setUniqueName(uniqueName);
options.setRepositoryKey(secretKey);
options.setVisibility(Visibility.PROTECTED);
ecmSvc.createRepository(options);
// should be created now, so connect to it
openCmisSession = ecmSvc.connect(uniqueName, secretKey);
}
return openCmisSession;
}
// local classes
class result {
String message;
void setMessage(String message) {
this.message = message;
}
}
class uploadData{
String file;
String fileName;
String folderName;
void setFile(String file) {
this.file = file;
}
void setFileName(String fileName) {
this.fileName = fileName;
}
void setFolderName(String folderName) {
this.folderName = folderName;
}
}
/**
* Creates the folder and file
* @param upload
* @return
*/
@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response create(uploadData upload) {
result r = new result();
String message = "";
Session openCmisSession = getSession();
// access the root folder of the repository
Folder root = openCmisSession.getRootFolder();
// create a new folder
try {
Map<String, String> newFolderProps = new HashMap<String, String>();
newFolderProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:folder");
newFolderProps.put(PropertyIds.NAME, upload.folderName);
root.createFolder(newFolderProps);
message = message + "Folder "+upload.folderName + " created in root." + "\n";
} catch (CmisNameConstraintViolationException e) {
// Folder exists already, nothing to do
message = message + "Folder "+upload.folderName + " already exists." + "\n";
}
// create a new file in the folder
List<Tree<FileableCmisObject>> folderTree = root.getFolderTree(1);
Tree<FileableCmisObject> treeo = folderTree.get(0);
Folder f = (Folder)treeo.getItem();
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
try {
properties.put(PropertyIds.NAME, upload.fileName);
byte[] fileContent;
fileContent = upload.file.getBytes("UTF-8");
InputStream stream = new ByteArrayInputStream(fileContent);
ContentStream contentStream = openCmisSession.getObjectFactory().createContentStream(upload.fileName,
fileContent.length, "text/plain; charset=UTF-8", stream);
f.createDocument(properties, contentStream, VersioningState.NONE);
message = message + "File "+upload.fileName + " created under Folder " + f.getName() + ".\n";
} catch (CmisNameConstraintViolationException e) {
// Document exists already, nothing to do
message = message + "File "+upload.fileName + " already exists in the folder "+ f.getName() + ".\n";
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
r.setMessage(e1.getMessage());
return Response.ok(r, MediaType.APPLICATION_JSON).build();
}
r.setMessage(message);
return Response.ok(r, MediaType.APPLICATION_JSON).build();
}
onHandleUploadPress: function (oEvent) {
var oFileUpload = this.getView().byId("fileUploader");
var domRef = oFileUpload.getFocusDomRef();
var file = domRef.files[0];
var that = this;
//Get the file name and type
this.fileName = file.name;
this.fileType = file.type;
//get the order number to be used as folder name
this.folderName = this.getModel("detailView").getData().sObjectId;
var reader = new FileReader();
reader.onload = function (e) {
var content = e.currentTarget.result.replace("data:" + file.type + ";base64,", "");
//call the method to make the ajax call to the webservice,
that.sendFiletoCloud(that.folderName, that.fileName, content);
};
reader.readAsDataURL(file);
},
sendFiletoCloud: function (folderName, fileName, file) {
jQuery.ajax({
url: "/clouddocumentservice/managedocuments/create",
type: "POST",
data: JSON.stringify({
folderName: folderName,
fileName: fileName,
file: file
}),
dataType: "json",
contentType: "application/json",
success: function (data) {
MessageToast.show("File uploaded successfully");
},
error: function (e) {
MessageToast.show("Error while uploading the file");
}
});
},
@GET
@Path("/read")
public String getobjects() {
String returnvalue = "";
Session openCmisSession = getSession();
returnvalue = "You are connected to the repository: " + openCmisSession.getRepositoryInfo().getId() + "\n";
// access the root folder of the repository
Folder root = openCmisSession.getRootFolder();
ItemIterable<CmisObject> children = root.getChildren();
for (CmisObject o : children) {
if (o instanceof Folder) {
Folder f = (Folder)o;
returnvalue = returnvalue + "Folder Name: " + o.getName() + "\n";
ItemIterable<CmisObject> Fchildren = f.getChildren();
for (CmisObject c : Fchildren) {
if(c instanceof Document) {
Document doc = (Document) c;
returnvalue = returnvalue + "File Name: " + doc.getName() + "---" + " filesize: "
+ doc.getContentStreamLength() + " bytes"+ "\n";;
}
}
} else {
Document doc = (Document) o;
returnvalue = returnvalue + " createdBy: " + o.getCreatedBy() + " filesize: "
+ doc.getContentStreamLength() + " bytes" + "\n";;
}
}
return returnvalue;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
11 | |
11 | |
8 | |
7 | |
5 | |
4 | |
4 | |
4 | |
3 |