Show TOC Start of Content Area

Background documentation Creating and Reading a KM Document from CAF Services  Locate the document in its SAP Library structure

Use

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.

Procedure

       1.      Create a new application:

                            a.      Choose File New Project and navigate to Development Infrastructure Development Component and choose Next.

                            b.      From the sap.com tree, select Composite Application and choose Next.

                            c.      Navigate to Local Development MyComponents [demo.sap.com]

                            d.      Enter kmdoc for a name of the application.

       2.      Create a new business object node:

                            a.      Click the node of modeled package with the secondary mouse button and choose New Business Object.

                            b.      Enter KMStudent for a name of the business object node and choose Finish.

       3.      Create attributes for your business object and an association to the KM business object Document:

                            a.      Open the Associations tab page of KMStudent business object.

                            b.      In the caf.core tree navigate to the KM business object Document and choose This graphic is explained in the accompanying text with the quick info text Add Dependency.

                            c.      Click the new association and enter documents.

                            d.      Open the Structure tab page and choose Edit Main Structure.

                            e.      In the caf.core tree, navigate to the primitive package and choose STRING and then choose This graphic is explained in the accompanying text with the quick info text Add Dependency. Click the new attribute and enter id.

                              f.      Create an attribute name and on the Structure tab page enable the Custom Key indicator for the attribute id.

       4.      Create an application service:

                            a.      Click the node of the modeled package with the secondary mouse button and choose New Application Service.

                            b.      Enter Manager for a name of the application service and choose Finish.

       5.      Create an operation for the application service and define operation parameters:

                            a.      Open the Operations tab page, choose Add and enter createStudentwithDoc for a name of the operation.

                            b.      In the caf.core tree, from the primitive package select the data type STRING, choose Add to Input and enter a name for the parameter docFilePath.

                            c.      Define two more input parameter of type STRING: studentId, studentName.

                            d.      In the caf.core tree, in the services package, navigate to the simple data type Rid, choose Add to Output and enter a name for the parameter Id.

                            e.      In the caf.core tree, in the faults package navigate to CAFCreateException and CAFUpdateException and choose Add to Fault.

       6.      Create a dependency for your application service:

                            a.      Open the Dependency tab page of your application service.

                            b.      In the caf.core tree, navigate to the business object Document and choose This graphic is explained in the accompanying text with the quick info text Add Dependency.

                            c.      Create a dependency to the application service DocContent.

       7.      Implement custom code in your application service operation:

                            a.      Open the Implementation tab page.

                            b.      Copy-paste the following code in the implementation of the application service operation createStudentwithDoc:

 

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();

}

       8.      To test your application in the Service Browser, save, build and deploy it.

End of Content Area