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

📄 actions.js

📁 Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是供中,大型企业来管理其发布在互连网
💻 JS
📖 第 1 页 / 共 4 页
字号:
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
 * actions.js
 *
 * Action layer, containing functions called after the actionsform is submitted.
 *
 */
 
importClass(Packages.org.apache.cocoon.components.modules.input.InputModule);
importClass(Packages.java.io.BufferedOutputStream);
importClass(Packages.java.io.FileOutputStream);
importClass(Packages.java.io.File);
importClass(Packages.java.io.StringReader);
importClass(Packages.java.io.OutputStreamWriter);
importClass(Packages.java.lang.Integer);

importClass(Packages.java.util.Calendar);

//Fileimport
importClass(Packages.nl.hippo.cms.fileimport.FileImporter);
importClass(Packages.nl.hippo.cocoon.repository.RepositoryManager);
importClass(Packages.nl.hippo.cocoon.webdav.WebDAVHelper);

importClass(Packages.nl.hippo.uidgenerator.UIDGenerator);
importClass(Packages.nl.hippo.util.FileDeletionTask);

importClass(Packages.org.apache.cocoon.components.cron.JobScheduler);
importClass(Packages.org.apache.excalibur.store.Store);

importClass(Packages.org.apache.cocoon.caching.EventAwareManager);

// locking
importClass(Packages.org.apache.webdav.lib.Lock);
importClass(Packages.org.apache.webdav.lib.methods.LockMethod);

cocoon.load("site:/config/config.js");
cocoon.load("site:/workflow/workflow.js");
cocoon.load("site:/util/flow/lib-io.js");
cocoon.load("site:/avalon/avalon.js");
cocoon.load("site:/spring/spring.js");

// content model
cocoon.load("site://actions/flow/contentmodel.js");

// optionally load custom actions flowscript from extensions
if (libio.checkExists("extensions://actions/flow/customactions.js"))
{
  cocoon.load("extensions://actions/flow/customactions.js");
}


// globals
var lastResponseStatus = 0; // holds the last returned dav status code
//var defaultWorkflowName = "MultipleSitesDocument";
//var defaultWorkflowName = "TaxonomyDocument";
//var defaultWorkflowName = "TaxonomyDatedDocument";
//var defaultWorkflowName = "ReviewedActionsThreeRepos";
var defaultWorkflowName = "ReviewedActions";


/**
 * Calls the supplied processor function, which does something with the
 * content model. In case of a ContentModelException, the webdav
 * response status is stored in the global variable lastResponseStatus
 * for use by the GUI to show an error message.
 *
 * @param   processor  a function that possibly throws a ContentModelException
 */
function handleContentModelException(processor)
{
  var result = true;
  lastResponseStatus = 0;
  try
  {
    processor();
  }
  catch(e)
  {
    result = false;
    if (e instanceof JavaException)
    {
      e = e.javaException; // unwrap java exception
    }
    if (e instanceof ContentModelException)
    {
      cocoon.log.error(e);
      if (e.hasInfo())
      {
        lastResponseStatus = e.getInfo();
      }
    }
    else
    {
      throw e;
    }
  }
  return result;
}


/**
 * Deletes the resource specified by resourceId.
 *
 * @param   uriPrefix    webdav files URL
 * @param   resourceId   path to the resource, relative to uriPrefix
 * @param   params       HashMap containing the parameters for this action
 *
 * @returns boolean
 */
function deleteResource(uriPrefix, resourceId, params) 
{
  var result = false;
  var resourceType = WebDAVHelper.propfindAsString(uriPrefix + resourceId, "DAV:", "resourcetype", authentication.getHttpState());
  if (resourceType != null)
  {
    resourceType = resourceType.toLowerCase();
    if (resourceType.indexOf("collection") == -1 && resourceId.indexOf("/content/") == 0) // resource is a document
    {
      result = handleContentModelException(function(){
        usingDocumentDo(uriPrefix, resourceId, function(document){
        
          // if trash bin is configured, move document and its versions to trash repository
          // TODO move this code to Document class
          var globalConstants = cocoon.getComponent(InputModule.ROLE + "Selector").select("globalconstants");
          var useTrashBin = globalConstants.getAttribute("cms-use-trash-bin", null, null);
          if (useTrashBin.equals("on"))
          {
            document.trash();
          }
          
          document.remove();        
          document.commitChanges();
        });
      });
    }
    else // resource is not a document
    {
      // delete the resource
      result = handleContentModelException(function(){
        usingResourceDo(uriPrefix, resourceId, function(resource){
          resource.remove();
          resource.commitChanges();
        });
      });
    }
  }
  return result;
}


/**
 * Deletes the collection specified by resourceId ( by calling deleteResource() ).
 *
 * @param   uriPrefix    webdav files URL
 * @param   resourceId   path to the collection to delete, relative to uriPrefix
 * @param   params       HashMap containing the parameters for this action
 *
 * @returns boolean
 */
function deleteCollection(uriPrefix, resourceId, params) {
  return (deleteResource(uriPrefix, resourceId, params));
}


/**
 * Returns the next index for a new resource.
 *
 * @param   parentId     path to the collection in which the new resource will be put
 */
function nextIndex(uriPrefix, parentId) {
  if (parentId == undefined)
  {
    parentId = "";
  }
  var index;
  var result = handleContentModelException(function(){
    usingCollectionDo(uriPrefix, parentId, function(collection){
      index = collection.nextChildIndex();
    });
  });
  if (result)
  {
    index = "00000000000" + index;
    index = index.substr(index.length - 11, 11);
  }
  else
  {
    index = "000000000000";
  }
  return index;
}


/**
 * Creates a new collection in the collection specified by parentid.
 *
 * @param   uriPrefix    webdav files URL
 * @param   parentId     path to the collection to put the new collection in, relative to uriPrefix
 * @param   params       HashMap containing the parameters for this action
 *
 * @returns boolean
 */
function makeCollection(uriPrefix, parentId, params) {
  var name = params.get("name");

  if (name == null)
  {
    if (cocoon.log.isDebugEnabled()) 
    {
      cocoon.log.debug("actions.js: makeCollection(): name attribute is missing. A collection name is required when adding a new document.");
    }
    lastResponseStatus = "explorer.error.actionfailed.collection.missing.name";
    return false;
  }

  var cleanName = spring.getBean("nameUtil").convertToValidName(name);
  cleanName = cleanName.toLowerCase();
  
  // form uri for new collection
  var resourceId;
  if (parentId.lastIndexOf('/') == parentId.length - 1) { // trailing slash
    resourceId = parentId + cleanName;
  }
  else {
    resourceId = parentId + "/" + cleanName;
  }
  var uri = uriPrefix + resourceId;

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

  var result = handleContentModelException(function(){
    usingRepositoryDo(function (repository) {
      var collection = repository.createCollection(uri);
      collection.setProperty("caption", name);
      collection.commitChanges(); 
    });
  });
  return result;
}

function publishIndexCollection(uriPrefix, parentId, params) {
  var resourcesWithIndex;

  var repoConfig = config.getCurrentRepository();
  var root = repoConfig.getRoot();
 
  var previewRoot = repoConfig.getRootPath() + repoConfig.getFilesPath();
  var liveRoot = previewRoot.substring(repoConfig.getRootPath().length(), previewRoot.lastIndexOf(".preview")) + ".www";  //TODO: do this in a clean way

  var result = handleContentModelException(function(){
    usingCollectionDo(uriPrefix, parentId, function(collection){
      
      resourcesWithIndex = collection.getChildIndices();
      usingRepositoryDo(function(repository){
          
        var it = resourcesWithIndex.keySet().iterator();
        while (it.hasNext()) {
          var key = it.next();
          var resourcePath = key.replaceFirst(previewRoot,liveRoot);
          
          try {
            var res = repository.lookupResource(root + resourcePath);
            res.setProperty("index", resourcesWithIndex.get(key));
            res.commitChanges();
          }
          catch(e) {
            //resource not found
            //print(e.getMessage());
          } 
        }      
      });      
    });
  });
  
  return result;    
}

/**
 * publishProperties
 * Makes a copy of all hippo-namespace properties on a resource from preview to live
 *
 * @param   uriPrefix    webdav files URL
 * @param   resourceId   path to the document or collection for which to create the workflow, relative to uriPrefix
 *
 * @returns boolean
 */
function publishProperties(uriPrefix, resourceId) {

  // the preview and resource path of the collection from which we want to publish the properties  
  var previewResource = uriPrefix + resourceId;
  var liveResource = uriPrefix2liveUriPrefix(uriPrefix) + resourceId;  //TODO: do this in a clean way


  /* PLEASE FIX THIS, IT DOES NOT WORK ON SOME ENVIRONMENTS NOW, BUT WOULD BE NICE
  if (libio.checkExists(liveUri)) {
    cocoon.log.error("actions.js: publishProperties: is called on " + livePath + ", but does not exist. Folder needs to be published to live before this method can be used.");
    return var result = false;
  }
  */
  
  // get all the properties from the collection
  var properties = WebDAVHelper.propfindTotal(previewResource, authentication.getHttpState());
  
  // define set for the properties created in the loop
  var set = new ArrayList();
   
    var iterator = properties.keySet().iterator();
    while (iterator.hasNext()) {
    
        var key = iterator.next();
        var name = properties.get(key).getLocalName();
        var namespace = properties.get(key).getNamespaceURI();
        var value = ""+properties.get(key).getPropertyAsString();

        // we only want to set the property if it uses the hippo-namespace, 
        // otherwise we can get authentication errors       
        if (namespace == "http://hippo.nl/cms/1.0") {            set.add(new Property("property", namespace, name, value));
        }
    }

    // set the properties on the live collection with proppatch
    var result; 
    if (WebDAVHelper.proppatch(liveResource, set, null, authentication.getHttpState())) {
      result = true;    } else {
      result = false;      cocoon.log.error("actions.js: publishProperties: proppatch failed on " + livePath);    }
    
    return result;
            
}


/**
 * Creates a new document by copying a specified XML document 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 newDocument(uriPrefix, parentId, params) {
  var name = params.get("name"); // new document name
  var type = params.get("type"); // new document type
  var xml = params.get("xml"); // URI of initial XML for this document type
  var workflowNameToUse;

⌨️ 快捷键说明

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