Show TOC

Creating and Reading a KM Document from CAF ServicesLocate this document in the navigation 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:
    1. Choose Start of the navigation path File Next navigation step New Next navigation step Project End of the navigation path and navigate to Start of the navigation path Development Infrastructure Next navigation step  Development Component  End of the navigation path and choose Next.
    2. From the sap.comtree, select Composite Applicationand choose Next.
    3. Navigate to Local Development → MyComponents [demo.sap.com]
    4. Enter kmdoc for a name of the application.
  2. Create a new business object node:
    1. Click the node of modeled package with the secondary mouse button and choose New Business Object.
    2. 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:
    1. Open the Associations tab page of KMStudent business object.
    2. In the caf.core tree navigate to the KM business object Document and choose with the quick info text Add Dependency.
    3. Click the new association and enter documents .
    4. Open the Structure tab page and choose Edit Main Structure.
    5. In the caf.core tree, navigate to the primitive package and choose STRING and then choose with the quick info text Add Dependency. Click the new attribute and enter id .
    6. Create an attribute name and on the Structure tab page enable the Custom Key indicator for the attribute id .
  4. Create an application service:
    1. Click the node of the modeled package with the secondary mouse button and choose New Application Service.
    2. Enter Manager for a name of the application service and choose Finish.
  5. Create an operation for the application service and define operation parameters:
    1. Open the Operations tab page, choose Add and enter createStudentwithDoc for a name of the operation.
    2. 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.
    3. Define two more input parameter of type STRING : studentId, studentName .
    4. In the caf.core tree, in the servicespackage, navigate to the simple data type Rid , choose Add to Output and enter a name for the parameter Id .
    5. 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:
    1. Open the Dependency tab page of your application service.
    2. In the caf.core tree, navigate to the business object Document and choose with the quick info text Add Dependency.
    3. Create a dependency to the application service DocContent.
  7. Implement custom code in your application service operation:
    1. Open the Implementation tab page.
    2. 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.