星期二, 6月 19, 2012

[Alfresco] How to create Java backed web script in alfresco (3)

這個範例在Google都找的到,沒做多大的修改。
重點是記錄Spring Bean XML的差異性。
Spring Bean XML 
重點在於property標籤設定,如果你參考了foundation的api請記得把他加進來!!,
所以這個範例要使用了Repository,請設定rel=repositoryHelper。
 <bean
         id="webscript.org.example.simplejavadir.get"
         class="org.example.SimpleJavaDir"
         parent="webscript">
                <property name="repository" ref="repositoryHelper"/>
        </bean>

Java backed controller
透過xml的設定, 就可以透過 public void setRepository(Repository repository) {}直接呼叫alfresco的核心api了。
package org.example;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;

public class SimpleJavaDir extends DeclarativeWebScript {
 
 private Repository repository;
 
    private static Log s_logger = LogFactory.getLog(SimpleJavaDir.class);


 public void setRepository(Repository repository) {
  this.repository = repository;
 }

 @Override
 protected Map<string, object>  executeImpl(WebScriptRequest req,
   Status status, Cache cache) {

  s_logger.debug("SimpleJavaDir entry");
  
  // extract folder listing arguments from URI
  String verboseArg = req.getParameter("verbose");
  Boolean verbose = Boolean.parseBoolean(verboseArg);
  Map<string, string>  templateArgs = req.getServiceMatch().getTemplateVars();
  String folderPath = templateArgs.get("folderpath");
  String nodePath = "workspace/SpacesStore/" + folderPath;

//  @param1 one of "node", "path", or "avmpath"
  NodeRef folder = repository.findNodeRef("path", nodePath.split("/"));
  // validate that folder has been found
  if (folder == null) {  
   throw new WebScriptException(Status.STATUS_NOT_FOUND, "Folder "
     + folderPath + " not found");
   //you will see the followning messages in your server
//    ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 05190001 Folder Company Homes not found

  }
  
  // construct model for response template to render
   Map<string, object> model = new HashMap<string, object>();
  model.put("verbose", verbose);
  model.put("folder", folder);
  model.put("nodePath", nodePath);
  return model;
 }
}


沒有留言:

張貼留言

留個話吧:)