⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 actions.js

📁 Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是供中,大型企业来管理其发布在互连网
💻 JS
📖 第 1 页 / 共 4 页
字号:

  // optional caption for new document
  var caption = params.get("caption");
  if (caption == null)
  {
    caption = name;
  }
  
  if (params.get("workflowName") == null || params.get("workflowName") == '')
  {
    workflowNameToUse = defaultWorkflowName; 
  }
  else
  {
    workflowNameToUse = params.get("workflowName");     
  }

  if (name == null)
  {
    if (cocoon.log.isDebugEnabled()) 
    {
      cocoon.log.debug("actions.js: newDocument(): name attribute is missing. A document name is required when adding a new document.");
    }
    lastResponseStatus = "explorer.error.actionfailed.document.missing.name";
    return false;
  }
  if (type == null)
  {
  if (cocoon.log.isDebugEnabled()) 
    {
      cocoon.log.debug("actions.js: newDocument(): document type '" + type + "' is null.");
    }
    lastResponseStatus = "explorer.error.actionfailed.document.missing.type";
    return false;
  }
  if (xml == null)
  {
    if (cocoon.log.isDebugEnabled()) 
    {
      cocoon.log.debug("actions.js: newDocument(): initial XML of document-type '" + type +"' is missing.");
    }
    lastResponseStatus = "explorer.error.actionfailed.document.missing.xml";
    return false;
  }


  var cleanName = spring.getBean("nameUtil").convertToValidName(name);

  // form uri for new resource
  var resourceId;
  if (parentId.lastIndexOf('/') == parentId.length - 1) { // trailing slash
    resourceId = parentId + cleanName + ".xml";
  }
  else {
    resourceId = parentId + "/" + cleanName + ".xml";
  }
  var uri = uriPrefix + resourceId;

  //check if webdav pathname is longer then 255 chars
  if (uri.length > 255) {
  if (cocoon.log.isDebugEnabled()) {
      cocoon.log.debug("actions.js: newDocument(): Pathname is to long:" + uri);
    }
    lastResponseStatus = "explorer.error.actionfailed.document.pathname.length";
    return false;
  }

  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: newDocument: uri=" + uri + ", type=" + type + ", xml=" + xml);
  }

  var result = handleContentModelException(function(){
    usingRepositoryDo(function(repository){
      var document = repository.createDocument(uri);
      
      usingInputStreamDo(xml, function(input){
        document.setContents(input);
        document.commitChanges();
      });
  
      document.setProperty("caption", caption);
      document.setProperty("type", type);
      document.setProperty("UID", UIDGenerator.generateUID());

      // Commit the changes here so the workflow can access the document
      // and its properties
      document.commitChanges();
  
      var workflowId = workflow.createWorkflow(resourceId, workflowNameToUse, uriPrefix);
      if (cocoon.log.isDebugEnabled()) {
        cocoon.log.debug("actions.js: newDocument(): workflowId: " + workflowId);
      }
      document.setProperty("workflowId", workflowId);
      document.commitChanges();
    });
  });
  return result;  
}

/**
 * Imports an uploaded OOO document as xml into the collection specified
 * by parentId.
 *
 * @param   uriPrefix    webdav files URL
 * @param   parentId     path to the collection to put the new document in, relative to uriPrefix
 * @param   params       HashMap containing the parameters for this action
 *
 * @returns boolean
 */
function importDocument(uriPrefix, parentId, params) {
  var name = params.get("name"); // new document name
  var type = params.get("type"); // new document type
  var file = params.get("file"); //source of uploaded file
  var binariesPath = params.get("binaries"); // binaries path

  var workflowNameToUse;
  if (params.get("importWorkflowName") == null || params.get("importWorkflowName") == '')
  {
    workflowNameToUse = defaultWorkflowName; 
  }
  else
  {
    workflowNameToUse = params.get("importWorkflowName");     
  }

  if (name == null)
  {
      if (cocoon.log.isDebugEnabled()) 
      {
         cocoon.log.error("actions.js: importDocument(): name attribute is missing. A document name is required when adding a new document.");
      }
    lastResponseStatus = "explorer.error.actionfailed.document.missing.name";
    return false;
  }
  if (type == null)
  {
      if (cocoon.log.isDebugEnabled()) 
      {
         cocoon.log.debug("actions.js: newDocument(): document type '" + type + "' is null.");
      }
      lastResponseStatus = "explorer.error.actionfailed.document.missing.type";
      return false;
  } 
  if (file == null)
  {
      if (cocoon.log.isDebugEnabled()) 
      {
         cocoon.log.error("actions.js: importDocument(): import file is null");
      }
      lastResponseStatus = "explorer.error.actionfailed.document.missing.file";
      return false;
  }   

  var cleanName = spring.getBean("nameUtil").convertToValidName(name);

  // form uri for new resource
  var resourceId;
  if (parentId.lastIndexOf('/') == parentId.length - 1) { // trailing slash
    resourceId = parentId + cleanName + ".xml";
  }
  else {
    resourceId = parentId + "/" + cleanName + ".xml";
  }
  var uri = uriPrefix + resourceId; // uri of parent collection of new document

  if(cocoon.log.isDebugEnabled()){
    cocoon.log.debug("actions.js: importDocument: name=" + cleanName + ", type=" + type + ", file=" + file + ", uri=" + uri);
  }  
  var fileImporter = null;
  try {
    fileImporter = cocoon.getComponent(FileImporter.ROLE);

    var configuration = config.getCurrentRepository();

    // TODO: getBinariesPath returns wrong path, temporary hardcoded fix
    //var binariesRoot = configuration.getRoot() + configuration.getBinariesPath();
    var binariesRoot = configuration.getRoot() + configuration.getFilesPath() + "/binaries";
    
    if (binariesPath == null || binariesPath == "") {
      binariesPath = "/";
    }

    if(cocoon.log.isDebugEnabled()){
      cocoon.log.debug("actions.js: importDocument: file: " + file);
      cocoon.log.debug("actions.js: importDocument: type: " + type);
      cocoon.log.debug("actions.js: importDocument: uri: " + uri);
      cocoon.log.debug("actions.js: importDocument: binariesRoot: " + binariesRoot);
      cocoon.log.debug("actions.js: importDocument: binariesPath: " + binariesPath);
    }
    
    fileImporter.importDocument(file, type, uri, binariesRoot, binariesPath);
    
  } 
  catch(e)
  {
    cocoon.log.error("actions.js: importDocument(): " + e);
    lastResponseStatus = e.getMessage();
    return false;
  }
  finally {
    if (fileImporter != null) {
      cocoon.releaseComponent(fileImporter);
    }
  }  

  var result = handleContentModelException(function(){
    usingDocumentDo(uriPrefix, resourceId, function(document){
      var workflowId = workflow.createWorkflow(resourceId, workflowNameToUse, uriPrefix);
      document.setProperty("workflowId", workflowId);
      document.setProperty("caption", name);
      document.setProperty("UID", UIDGenerator.generateUID());
      document.saveNewIndexProperty();
      document.commitChanges();
    });
  });
  return result;
}


function post_proppatch(status) {
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: post_proppatch: status=" + status);
  }
  lastResponseStatus = status;
}


function replace(uriPrefix, parentId, params) {
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: replace(): uriPrefix=" + uriPrefix);
    cocoon.log.debug("actions.js: replace(): parentId=" + parentId);
    cocoon.log.debug("actions.js: replace(): params=" + params);
  }
    
  var file = params.get("File"); // new document name
  var uri = uriPrefix + parentId; // uri of asset to replace with upload
  
  if(cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: replace(): file=" + file);
    cocoon.log.debug("actions.js: replace(): uri=" + uri);
  }	  

  if (file == null) {
    if (cocoon.log.isDebugEnabled()) {
      cocoon.log.debug("actions.js: replace(): the selected file is empty");
    }
    lastResponseStatus = "explorer.error.actionfailed.document.missing.file";
    return false;
  }

  var uploadedFile = new Packages.java.io.File(file);
  var result = handleContentModelException(function(){
    usingRepositoryDo(function (repository) {
      var asset = repository.lookupAsset(uri);
      asset.setFile(uploadedFile);
      asset.commitChanges();
    });
  });
  
  return true;
}

/**
 * Copies an uploaded file to the collection specified by parentId.
 *
 * @param   uriPrefix    webdav files URL
 * @param   parentId     path to the collection to put the new document in, relative to uriPrefix
 * @param   params       HashMap containing the parameters for this action
 *
 * @returns boolean
 */
function upload(uriPrefix, parentId, params) {
  if (cocoon.log.isDebugEnabled()) {
    cocoon.log.debug("actions.js: upload()");
  }
    
  var file = params.get("File"); // new document name
  var uri = uriPrefix + parentId; // uri of target collection for upload
  
  // create a new map for all uploads
  var fileMap = new HashMap();
  var nrOfFiles = parseInt("1");
  var uploadResults = new HashMap();
  
  fileMap.put(nrOfFiles,file);
  
  for(var i=1;i<=params.size();i++)
  { 
    if(params.containsKey("File"+i) && params.get("File"+i)!=null)
    { 
      nrOfFiles=nrOfFiles+1;
      fileMap.put((nrOfFiles), params.get("File"+i));
    }
  }
  
  var globalConstants = cocoon.getComponent(InputModule.ROLE + "Selector").select("globalconstants");
  var overwriteUploads = false; 
  var writeUploads = globalConstants.getAttribute("overwrite-uploads",null,null);
  
  if (writeUploads=='allow')
  {
    overwriteUploads = true;
  }
  // start iterating over uploads
  
  for(var j=1;j<=fileMap.size();j++)
  {
    var currentFile = fileMap.get(j);
  
    if (currentFile == null) {
      if (cocoon.log.isDebugEnabled()) {
        cocoon.log.debug("actions.js: upload(): One of the uploaded files is null.");
      }
      lastResponseStatus = "explorer.error.actionfailed.document.upload.missing.file";
      return false;
    }

    var uploadedFile = new Packages.java.io.File(currentFile);
    var name = uploadedFile.getName();
    if (cocoon.log.isDebugEnabled()) {
      cocoon.log.debug("actions.js: upload(): filename: " + name);
    }
  
    var cleanName = spring.getBean("nameUtil").convertToValidBinaryFilename(name);
    var newUri = uriPrefix + parentId;

    // form uri for new asset
    if (newUri.lastIndexOf('/') == newUri.length - 1) { // trailing slash
      newUri += cleanName;
    }
    else {
      newUri += "/" + cleanName;
    }

    if (cocoon.log.isDebugEnabled()) {
      cocoon.log.debug("actions.js: upload: uri=" + newUri);
    }

    if (currentFile == null) {
      if (cocoon.log.isDebugEnabled()) {
        cocoon.log.debug("upload: no file selected");
      }
      if (failureFunction != undefined) {
        failureFunction.call(this);
      }
      return;
    }

    if (cocoon.log.isDebugEnabled()) {
      cocoon.log.debug("actions.js: upload: filename: " + name);
      cocoon.log.debug("actions.js: upload: upload to: " + newUri);
    }
	
	var globalConstants = cocoon.getComponent(InputModule.ROLE + "Selector").select("globalconstants");	
    var useWorkflowOnAsset = globalConstants.getAttribute("cms-use-workflow-on-assets", null, null);
    useWorkflowOnAsset = (useWorkflowOnAsset != null && useWorkflowOnAsset.equals("on")) ? true:false;
	
    var result = handleContentModelException(function(){
      usingRepositoryDo(function (repository) {
        var asset = repository.createAsset(newUri,overwriteUploads);
        asset.setFile(uploadedFile);
        asset.setProperty("caption", name);
        if(useWorkflowOnAsset)
          asset.setProperty("type", "binary-resource");
        else
          asset.setProperty("type", "asset")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -