
The example below shows how to implement custom code in your composite application and how to model your application so that you can use the predefined KM services and business objects.
with the quick info text Add Dependency.
with the quick info text Add Dependency. Click the new attribute and enter id .
with the quick info text Add Dependency.public java.lang.String createStudentwithDoc(java.lang.String docFilePath,
java.lang.String StudentId,
java.lang.String StudentName)
throws com.sap.caf.rt.exception.CAFUpdateException, com.sap.caf.rt.exception.CAFCreateException {
String res = "";
DocumentServiceLocal docSvc = getDocumentService();
/* creates an empty document in the KM repository. Support for MIME types depends on
the file extension */
Document docCr = docSvc.create(getFileName(DocFilePath),null) ;
String docKey = docCr.getKey();
DocContentServiceLocal docContentLocal =getDocContentService();
try {
// uploads content by a given document key
docContentLocal.uploadDocument(docKey, getResFileContent(DocFilePath));
} catch (IOException e) {
throw new CAFUpdateException(e);
}
KMStudentServiceLocal studentLocal =getKMStudentService();
KMStudent student = studentLocal .create(StudentName,StudentId);
studentLocal.setDosuments(student.getKey(), new String[]{docCr.getKey()});
//associates a business object node to the document
docContentLocal.addRelatedObjectRid("",
docKey,student.getKey(),
KMStudent.class.getName());
Collection<String> rids = docContentLocal.getRelatedObjectRids(docKey);
if (rids.size() > 0){
res = (String) rids.toArray()[0];
}
return res;
}
private String getFileName(String fullPath){
String fileName = "";
int backSlashLastInx = fullPath.lastIndexOf("\\");
int slashLastIndx = fullPath.lastIndexOf("/");
if (backSlashLastInx == -1 && slashLastIndx ==-1){
throw new IllegalArgumentException("File path is not valid");
}
fileName = fullPath.substring(Math.max(slashLastIndx,backSlashLastInx) + 1);
return fileName;
}
private byte[] getResFileContent(String fileName) throws IOException {
int bl = 500;
URL url = new URL (fileName);
URLConnection urlConn = url.openConnection();
DataInputStream resIn = new DataInputStream(urlConn.getInputStream());
byte[] buff = new byte[bl];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
while ( resIn.read(buff) != -1 ){
bout.write(buff);
}
resIn.close();
return bout.toByteArray();
}