mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Merge from SEAMIST3
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@10720 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
218
source/java/org/alfresco/repo/cmis/CMISScript.java
Normal file
218
source/java/org/alfresco/repo/cmis/CMISScript.java
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.cmis;
|
||||
|
||||
import org.alfresco.repo.cmis.Navigation.TypesFilter;
|
||||
import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
|
||||
import org.alfresco.repo.jscript.ScriptNode;
|
||||
import org.alfresco.repo.web.scripts.Repository;
|
||||
import org.alfresco.repo.web.util.paging.Cursor;
|
||||
import org.alfresco.repo.web.util.paging.Page;
|
||||
import org.alfresco.repo.web.util.paging.PagedResults;
|
||||
import org.alfresco.repo.web.util.paging.Paging;
|
||||
import org.alfresco.service.ServiceRegistry;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
|
||||
|
||||
/**
|
||||
* CMIS Javascript API
|
||||
*
|
||||
* @author davic
|
||||
*/
|
||||
public class CMISScript extends BaseScopableProcessorExtension
|
||||
{
|
||||
private static final TypesFilter defaultTypesFilter = TypesFilter.FoldersAndDocuments;
|
||||
|
||||
|
||||
private ServiceRegistry services;
|
||||
private Repository repository;
|
||||
private Navigation navigation;
|
||||
private Paging paging;
|
||||
|
||||
/**
|
||||
* Set the service registry
|
||||
*
|
||||
* @param services the service registry
|
||||
*/
|
||||
public void setServiceRegistry(ServiceRegistry services)
|
||||
{
|
||||
this.services = services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the repository
|
||||
*
|
||||
* @param repository
|
||||
*/
|
||||
public void setRepository(Repository repository)
|
||||
{
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the paging helper
|
||||
*
|
||||
* @param paging
|
||||
*/
|
||||
public void setPaging(Paging paging)
|
||||
{
|
||||
this.paging = paging;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the CMIS Navigation helper
|
||||
*
|
||||
* @param navigation
|
||||
*/
|
||||
public void setNavigation(Navigation navigation)
|
||||
{
|
||||
this.navigation = navigation;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds the arg value from the specified url argument and header
|
||||
*
|
||||
* NOTE: Url argument takes precedence over header
|
||||
*
|
||||
* @param argVal url arg value
|
||||
* @param headerVal header value
|
||||
* @return value (or null)
|
||||
*/
|
||||
public String findArg(String argVal, String headerVal)
|
||||
{
|
||||
return (argVal == null) ? headerVal : argVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the arg value from the specified url argument and header
|
||||
*
|
||||
* NOTE: Url argument takes precedence over header
|
||||
*
|
||||
* @param argVal url arg value
|
||||
* @param headerVal header value
|
||||
* @return value (or null)
|
||||
*/
|
||||
public String[] findArgM(String[] argVal, String[] headerVal)
|
||||
{
|
||||
return (argVal == null) ? headerVal : argVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default Types filter
|
||||
*
|
||||
* @return default types filter
|
||||
*/
|
||||
public String getDefaultTypesFilter()
|
||||
{
|
||||
return defaultTypesFilter.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is specified Types filter valid?
|
||||
*
|
||||
* @param typesFilter types filter
|
||||
* @return true => valid
|
||||
*/
|
||||
public boolean isValidTypesFilter(String typesFilter)
|
||||
{
|
||||
try
|
||||
{
|
||||
TypesFilter.valueOf(typesFilter);
|
||||
return true;
|
||||
}
|
||||
catch(IllegalArgumentException e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch(NullPointerException e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a Node with the repository given a reference
|
||||
*
|
||||
* @param referenceType node, path
|
||||
* @param reference node => id, path => path
|
||||
* @return node (or null, if not found)
|
||||
*/
|
||||
public ScriptNode findNode(String referenceType, String[] reference)
|
||||
{
|
||||
ScriptNode node = null;
|
||||
NodeRef nodeRef = repository.findNodeRef(referenceType, reference);
|
||||
if (nodeRef != null)
|
||||
{
|
||||
node = new ScriptNode(nodeRef, services, getScope());
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query for node children
|
||||
*
|
||||
* @param parent node to query children for
|
||||
* @param typesFilter types filter
|
||||
* @param page page to query for
|
||||
* @return paged result set of children
|
||||
*/
|
||||
public PagedResults queryChildren(ScriptNode parent, String typesFilter, Page page)
|
||||
{
|
||||
TypesFilter filter = resolveTypesFilter(typesFilter);
|
||||
NodeRef[] children = navigation.getChildren(parent.getNodeRef(), filter);
|
||||
|
||||
Cursor cursor = paging.createCursor(children.length, page);
|
||||
ScriptNode[] nodes = new ScriptNode[cursor.getRowCount()];
|
||||
for (int i = cursor.getStartRow(); i <= cursor.getEndRow(); i++)
|
||||
{
|
||||
nodes[i - cursor.getStartRow()] = new ScriptNode(children[i], services, getScope());
|
||||
}
|
||||
|
||||
PagedResults results = paging.createPagedResults(nodes, cursor);
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve to a Types Filter
|
||||
*
|
||||
* NOTE: If specified types filter is not specified or invalid, the default types
|
||||
* filter is returned
|
||||
*
|
||||
* @param typesFilter types filter
|
||||
* @return resolved types filter
|
||||
*/
|
||||
private TypesFilter resolveTypesFilter(String typesFilter)
|
||||
{
|
||||
if (isValidTypesFilter(typesFilter))
|
||||
{
|
||||
return TypesFilter.valueOf(typesFilter);
|
||||
}
|
||||
else
|
||||
{
|
||||
return defaultTypesFilter;
|
||||
}
|
||||
}
|
||||
}
|
179
source/java/org/alfresco/repo/cmis/Navigation.java
Normal file
179
source/java/org/alfresco/repo/cmis/Navigation.java
Normal file
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Alfresco Software Limited.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
* As a special exception to the terms and conditions of version 2.0 of
|
||||
* the GPL, you may redistribute this Program in connection with Free/Libre
|
||||
* and Open Source Software ("FLOSS") applications as described in Alfresco's
|
||||
* FLOSS exception. You should have recieved a copy of the text describing
|
||||
* the FLOSS exception, and it is also available here:
|
||||
* http://www.alfresco.com/legal/licensing"
|
||||
*/
|
||||
package org.alfresco.repo.cmis;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.search.QueryParameterDefImpl;
|
||||
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
|
||||
import org.alfresco.service.cmr.dictionary.DictionaryService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.search.QueryParameterDefinition;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.SearchParameters;
|
||||
import org.alfresco.service.cmr.search.SearchService;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
|
||||
/**
|
||||
* CMIS Navigation Service
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public class Navigation implements InitializingBean
|
||||
{
|
||||
/**
|
||||
* Types Filter
|
||||
*
|
||||
* @author davidc
|
||||
*/
|
||||
public enum TypesFilter
|
||||
{
|
||||
Folders,
|
||||
FoldersAndDocuments,
|
||||
Documents
|
||||
};
|
||||
|
||||
/** Query Parameters */
|
||||
private static final QName PARAM_PARENT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "parent");
|
||||
|
||||
/** Shallow search for all files and folders */
|
||||
private static final String LUCENE_QUERY_SHALLOW_FOLDERS =
|
||||
"+PARENT:\"${cm:parent}\" " +
|
||||
"-TYPE:\"" + ContentModel.TYPE_SYSTEM_FOLDER + "\" " +
|
||||
"+TYPE:\"" + ContentModel.TYPE_FOLDER + "\" ";
|
||||
|
||||
/** Shallow search for all files and folders */
|
||||
private static final String LUCENE_QUERY_SHALLOW_FILES =
|
||||
"+PARENT:\"${cm:parent}\" " +
|
||||
"-TYPE:\"" + ContentModel.TYPE_SYSTEM_FOLDER + "\" " +
|
||||
"+TYPE:\"" + ContentModel.TYPE_CONTENT + "\" ";
|
||||
|
||||
// dependencies
|
||||
private DictionaryService dictionaryService;
|
||||
private SearchService searchService;
|
||||
private DataTypeDefinition nodeRefDataType;
|
||||
|
||||
|
||||
/**
|
||||
* @param dictionaryService
|
||||
*/
|
||||
public void setDictionaryService(DictionaryService dictionaryService)
|
||||
{
|
||||
this.dictionaryService = dictionaryService;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param searchService
|
||||
*/
|
||||
public void setSearchService(SearchService searchService)
|
||||
{
|
||||
this.searchService = searchService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialisation
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception
|
||||
{
|
||||
nodeRefDataType = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Query for node children
|
||||
*
|
||||
* @param parent node to query children for
|
||||
* @param typesFilter types filter
|
||||
* @return children of node
|
||||
*/
|
||||
public NodeRef[] getChildren(NodeRef parent, TypesFilter typesFilter)
|
||||
{
|
||||
if (typesFilter == TypesFilter.FoldersAndDocuments)
|
||||
{
|
||||
NodeRef[] folders = queryChildren(parent, TypesFilter.Folders);
|
||||
NodeRef[] docs = queryChildren(parent, TypesFilter.Documents);
|
||||
NodeRef[] foldersAndDocs = new NodeRef[folders.length + docs.length];
|
||||
System.arraycopy(folders, 0, foldersAndDocs, 0, folders.length);
|
||||
System.arraycopy(docs, 0, foldersAndDocs, folders.length, docs.length);
|
||||
return foldersAndDocs;
|
||||
}
|
||||
else if (typesFilter == TypesFilter.Folders)
|
||||
{
|
||||
NodeRef[] folders = queryChildren(parent, TypesFilter.Folders);
|
||||
return folders;
|
||||
}
|
||||
else if (typesFilter == TypesFilter.Documents)
|
||||
{
|
||||
NodeRef[] docs = queryChildren(parent, TypesFilter.Documents);
|
||||
return docs;
|
||||
}
|
||||
|
||||
return new NodeRef[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Query children helper
|
||||
*
|
||||
* NOTE: Queries for folders only or documents only
|
||||
*
|
||||
* @param parent node to query children for
|
||||
* @param typesFilter folders or documents
|
||||
* @return node children
|
||||
*/
|
||||
private NodeRef[] queryChildren(NodeRef parent, TypesFilter typesFilter)
|
||||
{
|
||||
SearchParameters params = new SearchParameters();
|
||||
params.setLanguage(SearchService.LANGUAGE_LUCENE);
|
||||
params.addStore(parent.getStoreRef());
|
||||
QueryParameterDefinition parentDef = new QueryParameterDefImpl(PARAM_PARENT, nodeRefDataType, true, parent.toString());
|
||||
params.addQueryParameterDefinition(parentDef);
|
||||
|
||||
if (typesFilter == TypesFilter.Folders)
|
||||
{
|
||||
params.setQuery(LUCENE_QUERY_SHALLOW_FOLDERS);
|
||||
}
|
||||
else if (typesFilter == TypesFilter.Documents)
|
||||
{
|
||||
params.setQuery(LUCENE_QUERY_SHALLOW_FILES);
|
||||
}
|
||||
|
||||
ResultSet resultSet = searchService.query(params);
|
||||
try
|
||||
{
|
||||
List<NodeRef> results = resultSet.getNodeRefs();
|
||||
NodeRef[] nodeRefs = new NodeRef[results.size()];
|
||||
return results.toArray(nodeRefs);
|
||||
}
|
||||
finally
|
||||
{
|
||||
resultSet.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -135,6 +135,28 @@ public class Paging
|
||||
return zeroBasedRow;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Page or Window
|
||||
*
|
||||
* @param pageNumber page number (optional and paired with pageSize)
|
||||
* @param pageSize page size (optional and paired with pageNumber)
|
||||
* @param skipCount skipCount (optional and paired with maxItems)
|
||||
* @param maxItems maxItems (optional and paired with skipCount)
|
||||
* @return page (if pageNumber driven) or window (if skipCount driven)
|
||||
*/
|
||||
public Page createPageOrWindow(Integer pageNumber, Integer pageSize, Integer skipCount, Integer maxItems)
|
||||
{
|
||||
if (pageNumber != null)
|
||||
{
|
||||
return createPage(pageNumber, pageSize == null ? 0 : pageSize);
|
||||
}
|
||||
else if (skipCount != null)
|
||||
{
|
||||
return createWindow(skipCount, maxItems == null ? 0 : maxItems);
|
||||
}
|
||||
return createUnlimitedPage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Page
|
||||
*
|
||||
@@ -147,6 +169,16 @@ public class Paging
|
||||
return new Page(PageType.PAGE, zeroBasedPage, pageNumber, pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an unlimited Page
|
||||
*
|
||||
* @return page (single Page starting at first page of unlimited page size)
|
||||
*/
|
||||
public Page createUnlimitedPage()
|
||||
{
|
||||
return new Page(PageType.PAGE, zeroBasedPage, zeroBasedPage ? 0 : 1, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a Window
|
||||
* @param skipRows number of rows to skip
|
||||
|
Reference in New Issue
Block a user