WEB开发网
开发学院软件开发Java 开放源码 CMS 入门,第 3 部分: 构建定制存储 阅读

开放源码 CMS 入门,第 3 部分: 构建定制存储

 2010-04-16 00:00:00 来源:WEB开发网   
核心提示: 与 CreateDocument 方法不同,UpdateDocument 方法(见清单 6)需要确保它有一个 sforce ID(也叫做 SFID),开放源码 CMS 入门,第 3 部分: 构建定制存储(9),如果 SFID 还没有作为文档的属性存储下来,则首先必须执行一个查询

与 CreateDocument 方法不同,UpdateDocument 方法(见清单 6)需要确保它有一个 sforce ID(也叫做 SFID)。如果 SFID 还没有作为文档的属性存储下来,则首先必须执行一个查询,以便从 salesforce.com 获得它。

清单 6. UpdateDocument

 
public String UpdateDocument(NodeRev 
isionDescriptor revisionDescriptor, File doc){ 
   
  String docName = revisionDescriptor.getName(); 
  String description = revisionDescriptor.getETag(); 
  ID authorId = new ID("YourauthorIDString"); 
   
  ID docId = new ID((String)revisionDescrip 
  tor.getProperty("SFID").getValue()); 
     
    if (docId == null || docId.getValue().length() != 18) { 
      try { 
        QueryResult qr = binding.query( 
          "Select Id From Document"); 
 
        SObject[] recs = qr.getRecords(); 
        for (int i = 0; i < recs.length; i++) { 
          SObject object = recs[i]; 
          if (object instanceof Document) { 
            Document d = (Document) qr.getRecords(i); 
            if (d.getName().compareToIgnoreCase(docName) 
              == 0){ 
              docId = d.getId(); 
              break; 
            } 
          } 
        } 
        if (docId == null) throw new InvalidSObjectFault( 
             ExceptionCode.UNKNOWN_EXCEPTION, 
             "docId == null, 
             possibly doc " + docName + " not found"); 
      } catch (UnexpectedErrorFault e2) { 
        e2.printStackTrace(); 
      } catch (InvalidSObjectFault e2) { 
        e2.printStackTrace(); 
      } catch (MalformedQueryFault e2) { 
        e2.printStackTrace(); 
      } catch (InvalidFieldFault e2) { 
        e2.printStackTrace(); 
      } catch (RemoteException e2) { 
        e2.printStackTrace(); 
      } 
    } 
 
 
    System.out.println("//// UpdateDocument ////"); 
    System.out.println("docName=" + docName); 
    System.out.println("File doc=" + doc.toString()); 
    System.out.println("Description=" + description); 
    System.out.println("authorId=" + authorId.getValue()); 
    System.out.println("docId=" + docId.getValue()); 
 
    String retStatus = "Success"; 
    int fileLength = (int) doc.length(); 
    byte[] body = new byte[fileLength]; 
 
    //Verify that we are already authenticated, if not 
    //call the login function to do so 
    if (!loggedIn) { 
      if (!login()) { 
        return "Bad SalesForce Login"; 
      } 
    } 
    try { 
      FileInputStream fis = new FileInputStream(doc); 
      fileLength = fis.read(body); 
    } catch (FileNotFoundException e) { 
      retStatus = "Error Failed to UpdateDocument:" 
          + e.getLocalizedMessage(); 
      e.printStackTrace(); 
      return retStatus; 
    } catch (IOException e1) { 
      retStatus = "Error Failed to UpdateDocument:" 
          + e1.getLocalizedMessage(); 
      e1.printStackTrace(); 
      return retStatus; 
    } 
 
    if (authorId == null) { 
      //default to current user 
      authorId = this.loginResult.getUserId(); 
    } 
 
    try { 
      Document document; 
      SObject[] sObjects = new SObject[1]; 
      document = new Document(); 
 
      document.setName(docName); 
      document.setBody(body); 
      document.setDescription(description); 
      document.setAuthorId(authorId); 
      document.setId(docId); 
 
      sObjects[0] = document; 
 
      //create the object(s) by sending the array 
      //to the web service 
      SaveResult[] sr = binding.update(sObjects); 
      for (int i = 0; i < sr.length; i++) { 
        SaveResult result = sr[i]; 
        if (result.isSuccess()) { 
          retStatus = "Success Updated Document " + 
            docName; 
        } else { 
          Error[] ers = (Error[]) result.getErrors(); 
          retStatus = "Error:"; 
          for (int j = 0; j < ers.length; j++) { 
            Error error = ers[j]; 
            retStatus += error.getMessage() + ";"; 
            System.out.println("error.getStatusCode():" 
                + error.getStatusCode()); 
            System.out.println(retStatus); 
          } 
        } 
      } 
    } catch (UnexpectedErrorFault uef) { 
      this.getLogger().log(uef.getExceptionMessage()); 
    } catch (Exception ex) { 
      retStatus = "\nFailed to Update Document, " + docName 
          + ", error message was: \n" + ex.getMessage(); 
      System.out.println(retStatus); 
    } 
 
    return retStatus; 
  } 

上一页  4 5 6 7 8 9 10  下一页

Tags:开放 源码 CMS

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接