mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Big honkin' merge from head. Sheesh!
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/WCM-DEV2/root@3617 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -39,12 +39,16 @@ import org.alfresco.service.cmr.repository.ContentReader;
|
||||
import org.alfresco.service.cmr.repository.ContentService;
|
||||
import org.alfresco.service.cmr.repository.ContentWriter;
|
||||
import org.alfresco.service.cmr.repository.CopyService;
|
||||
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
|
||||
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
|
||||
import org.alfresco.service.cmr.repository.MimetypeService;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.NodeService;
|
||||
import org.alfresco.service.cmr.repository.Path;
|
||||
import org.alfresco.service.cmr.search.QueryParameterDefinition;
|
||||
import org.alfresco.service.cmr.search.ResultSet;
|
||||
import org.alfresco.service.cmr.search.ResultSetRow;
|
||||
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;
|
||||
@@ -59,24 +63,30 @@ import org.apache.commons.logging.LogFactory;
|
||||
*/
|
||||
public class FileFolderServiceImpl implements FileFolderService
|
||||
{
|
||||
/** Shallow search for all files */
|
||||
private static final String XPATH_QUERY_SHALLOW_FILES =
|
||||
"./*" +
|
||||
"[(subtypeOf('" + ContentModel.TYPE_CONTENT + "'))]";
|
||||
|
||||
/** Shallow search for all folder */
|
||||
private static final String XPATH_QUERY_SHALLOW_FOLDERS =
|
||||
"./*" +
|
||||
"[not (subtypeOf('" + ContentModel.TYPE_SYSTEM_FOLDER + "'))" +
|
||||
" and (subtypeOf('" + ContentModel.TYPE_FOLDER + "'))]";
|
||||
|
||||
/** Shallow search for all files and folders */
|
||||
/** Shallow search for files and folders with a name pattern */
|
||||
private static final String XPATH_QUERY_SHALLOW_ALL =
|
||||
"./*" +
|
||||
"[like(@cm:name, $cm:name, false)" +
|
||||
" and not (subtypeOf('" + ContentModel.TYPE_SYSTEM_FOLDER + "'))" +
|
||||
" and (subtypeOf('" + ContentModel.TYPE_FOLDER + "') or subtypeOf('" + ContentModel.TYPE_CONTENT + "'))]";
|
||||
|
||||
/** Shallow search for all files and folders */
|
||||
private static final String LUCENE_QUERY_SHALLOW_ALL =
|
||||
"+PARENT:\"${cm:parent}\"" +
|
||||
"-TYPE:\"" + ContentModel.TYPE_SYSTEM_FOLDER + "\" ";
|
||||
|
||||
/** 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 + "\" ";
|
||||
|
||||
/** Deep search for files and folders with a name pattern */
|
||||
private static final String XPATH_QUERY_DEEP_ALL =
|
||||
".//*" +
|
||||
@@ -85,8 +95,8 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
" and (subtypeOf('" + ContentModel.TYPE_FOLDER + "') or subtypeOf('" + ContentModel.TYPE_CONTENT + "'))]";
|
||||
|
||||
/** empty parameters */
|
||||
private static final QueryParameterDefinition[] PARAMS_EMPTY = new QueryParameterDefinition[0];
|
||||
private static final QueryParameterDefinition[] PARAMS_ANY_NAME = new QueryParameterDefinition[1];
|
||||
private static final QName PARAM_QNAME_PARENT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "parent");
|
||||
|
||||
private static Log logger = LogFactory.getLog(FileFolderServiceImpl.class);
|
||||
|
||||
@@ -100,6 +110,7 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
|
||||
// TODO: Replace this with a more formal means of identifying "system" folders (i.e. aspect or UUID)
|
||||
private List systemPaths;
|
||||
private DataTypeDefinition dataTypeNodeRef;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
@@ -157,6 +168,7 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
dictionaryService.getDataType(DataTypeDefinition.TEXT),
|
||||
true,
|
||||
"%");
|
||||
dataTypeNodeRef = dictionaryService.getDataType(DataTypeDefinition.NODE_REF);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -194,22 +206,6 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that a file or folder with the given name does not already exist
|
||||
*
|
||||
* @throws FileExistsException if a same-named file or folder already exists
|
||||
*/
|
||||
private void checkExists(NodeRef parentFolderRef, String name)
|
||||
throws FileExistsException
|
||||
{
|
||||
// check for existing file or folder
|
||||
List<FileInfo> existingFileInfos = this.search(parentFolderRef, name, true, true, false);
|
||||
if (existingFileInfos.size() > 0)
|
||||
{
|
||||
throw new FileExistsException(existingFileInfos.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exception when the type is not a valid File or Folder type
|
||||
*
|
||||
@@ -258,18 +254,10 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Use Lucene search to get file attributes without having to visit the node service
|
||||
*/
|
||||
public List<FileInfo> list(NodeRef contextNodeRef)
|
||||
{
|
||||
// execute the query
|
||||
List<NodeRef> nodeRefs = searchService.selectNodes(
|
||||
contextNodeRef,
|
||||
XPATH_QUERY_SHALLOW_ALL,
|
||||
PARAMS_ANY_NAME,
|
||||
namespaceService,
|
||||
false);
|
||||
List<NodeRef> nodeRefs = luceneSearch(contextNodeRef, true, true);
|
||||
// convert the noderefs
|
||||
List<FileInfo> results = toFileInfo(nodeRefs);
|
||||
// done
|
||||
@@ -282,18 +270,10 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Use Lucene search to get file attributes without having to visit the node service
|
||||
*/
|
||||
public List<FileInfo> listFiles(NodeRef contextNodeRef)
|
||||
{
|
||||
// execute the query
|
||||
List<NodeRef> nodeRefs = searchService.selectNodes(
|
||||
contextNodeRef,
|
||||
XPATH_QUERY_SHALLOW_FILES,
|
||||
PARAMS_EMPTY,
|
||||
namespaceService,
|
||||
false);
|
||||
List<NodeRef> nodeRefs = luceneSearch(contextNodeRef, false, true);
|
||||
// convert the noderefs
|
||||
List<FileInfo> results = toFileInfo(nodeRefs);
|
||||
// done
|
||||
@@ -306,18 +286,10 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO: Use Lucene search to get file attributes without having to visit the node service
|
||||
*/
|
||||
public List<FileInfo> listFolders(NodeRef contextNodeRef)
|
||||
{
|
||||
// execute the query
|
||||
List<NodeRef> nodeRefs = searchService.selectNodes(
|
||||
contextNodeRef,
|
||||
XPATH_QUERY_SHALLOW_FOLDERS,
|
||||
PARAMS_EMPTY,
|
||||
namespaceService,
|
||||
false);
|
||||
List<NodeRef> nodeRefs = luceneSearch(contextNodeRef, true, false);
|
||||
// convert the noderefs
|
||||
List<FileInfo> results = toFileInfo(nodeRefs);
|
||||
// done
|
||||
@@ -329,6 +301,20 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
public NodeRef searchSimple(NodeRef contextNodeRef, String name)
|
||||
{
|
||||
NodeRef childNodeRef = nodeService.getChildByName(contextNodeRef, ContentModel.ASSOC_CONTAINS, name);
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug(
|
||||
"Simple name search results: \n" +
|
||||
" parent: " + contextNodeRef + "\n" +
|
||||
" name: " + name + "\n" +
|
||||
" result: " + childNodeRef);
|
||||
}
|
||||
return childNodeRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #search(NodeRef, String, boolean, boolean, boolean)
|
||||
@@ -338,6 +324,7 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
return search(contextNodeRef, namePattern, true, true, includeSubFolders);
|
||||
}
|
||||
|
||||
private static final String LUCENE_MULTI_CHAR_WILDCARD = "*";
|
||||
/**
|
||||
* Full search with all options
|
||||
*/
|
||||
@@ -348,50 +335,9 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
boolean folderSearch,
|
||||
boolean includeSubFolders)
|
||||
{
|
||||
// shortcut if the search is requesting nothing
|
||||
if (!fileSearch && !folderSearch)
|
||||
{
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// get the raw nodeRefs
|
||||
List<NodeRef> nodeRefs = searchInternal(contextNodeRef, namePattern, fileSearch, folderSearch, includeSubFolders);
|
||||
|
||||
// if the name pattern is null, then we use the ANY pattern
|
||||
QueryParameterDefinition[] params = null;
|
||||
if (namePattern != null)
|
||||
{
|
||||
// the interface specifies the Lucene syntax, so perform a conversion
|
||||
namePattern = SearchLanguageConversion.convert(
|
||||
SearchLanguageConversion.DEF_LUCENE,
|
||||
SearchLanguageConversion.DEF_XPATH_LIKE,
|
||||
namePattern);
|
||||
|
||||
params = new QueryParameterDefinition[1];
|
||||
params[0] = new QueryParameterDefImpl(
|
||||
ContentModel.PROP_NAME,
|
||||
dictionaryService.getDataType(DataTypeDefinition.TEXT),
|
||||
true,
|
||||
namePattern);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = PARAMS_ANY_NAME;
|
||||
}
|
||||
// determine the correct query to use
|
||||
String query = null;
|
||||
if (includeSubFolders)
|
||||
{
|
||||
query = XPATH_QUERY_DEEP_ALL;
|
||||
}
|
||||
else
|
||||
{
|
||||
query = XPATH_QUERY_SHALLOW_ALL;
|
||||
}
|
||||
// execute the query
|
||||
List<NodeRef> nodeRefs = searchService.selectNodes(
|
||||
contextNodeRef,
|
||||
query,
|
||||
params,
|
||||
namespaceService,
|
||||
false);
|
||||
List<FileInfo> results = toFileInfo(nodeRefs);
|
||||
// eliminate unwanted files/folders
|
||||
Iterator<FileInfo> iterator = results.iterator();
|
||||
@@ -420,6 +366,133 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a full search, but doesn't translate the node references into
|
||||
* file info objects. This allows {@link #checkExists(NodeRef, String)} to
|
||||
* bypass the retrieval of node properties.
|
||||
*/
|
||||
private List<NodeRef> searchInternal(
|
||||
NodeRef contextNodeRef,
|
||||
String namePattern,
|
||||
boolean fileSearch,
|
||||
boolean folderSearch,
|
||||
boolean includeSubFolders)
|
||||
{
|
||||
// shortcut if the search is requesting nothing
|
||||
if (!fileSearch && !folderSearch)
|
||||
{
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (namePattern == null)
|
||||
{
|
||||
namePattern = LUCENE_MULTI_CHAR_WILDCARD; // default to wildcard
|
||||
}
|
||||
// now check if we can use Lucene to handle this query
|
||||
boolean useLucene = false;
|
||||
boolean anyName = namePattern.equals(LUCENE_MULTI_CHAR_WILDCARD);
|
||||
if (!includeSubFolders && anyName)
|
||||
{
|
||||
// Lucene only handles any name or exact name
|
||||
useLucene = true;
|
||||
}
|
||||
|
||||
List<NodeRef> nodeRefs = null;
|
||||
if (!useLucene) // go with the XPath queries
|
||||
{
|
||||
// if the name pattern is null, then we use the ANY pattern
|
||||
QueryParameterDefinition[] params = null;
|
||||
if (namePattern != null)
|
||||
{
|
||||
// the interface specifies the Lucene syntax, so perform a conversion
|
||||
namePattern = SearchLanguageConversion.convert(
|
||||
SearchLanguageConversion.DEF_LUCENE,
|
||||
SearchLanguageConversion.DEF_XPATH_LIKE,
|
||||
namePattern);
|
||||
|
||||
params = new QueryParameterDefinition[1];
|
||||
params[0] = new QueryParameterDefImpl(
|
||||
ContentModel.PROP_NAME,
|
||||
dictionaryService.getDataType(DataTypeDefinition.TEXT),
|
||||
true,
|
||||
namePattern);
|
||||
}
|
||||
else
|
||||
{
|
||||
params = PARAMS_ANY_NAME;
|
||||
}
|
||||
// determine the correct query to use
|
||||
String query = null;
|
||||
if (includeSubFolders)
|
||||
{
|
||||
query = XPATH_QUERY_DEEP_ALL;
|
||||
}
|
||||
else
|
||||
{
|
||||
query = XPATH_QUERY_SHALLOW_ALL;
|
||||
}
|
||||
// execute the query
|
||||
nodeRefs = searchService.selectNodes(
|
||||
contextNodeRef,
|
||||
query,
|
||||
params,
|
||||
namespaceService,
|
||||
false);
|
||||
}
|
||||
else // go with Lucene queries
|
||||
{
|
||||
nodeRefs = luceneSearch(contextNodeRef, folderSearch, fileSearch);
|
||||
}
|
||||
// done
|
||||
return nodeRefs;
|
||||
}
|
||||
|
||||
private List<NodeRef> luceneSearch(NodeRef contextNodeRef, boolean folders, boolean files)
|
||||
{
|
||||
SearchParameters params = new SearchParameters();
|
||||
params.setLanguage(SearchService.LANGUAGE_LUCENE);
|
||||
params.addStore(contextNodeRef.getStoreRef());
|
||||
// set the parent parameter
|
||||
QueryParameterDefinition parentParamDef = new QueryParameterDefImpl(
|
||||
PARAM_QNAME_PARENT,
|
||||
dataTypeNodeRef,
|
||||
true,
|
||||
contextNodeRef.toString());
|
||||
params.addQueryParameterDefinition(parentParamDef);
|
||||
if (folders && files) // search for both files and folders
|
||||
{
|
||||
params.setQuery(LUCENE_QUERY_SHALLOW_ALL);
|
||||
}
|
||||
else if (folders) // search for folders only
|
||||
{
|
||||
params.setQuery(LUCENE_QUERY_SHALLOW_FOLDERS);
|
||||
}
|
||||
else if (files) // search for files only
|
||||
{
|
||||
params.setQuery(LUCENE_QUERY_SHALLOW_FILES);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("Must search for either files or folders or both");
|
||||
}
|
||||
ResultSet rs = searchService.query(params);
|
||||
int length = rs.length();
|
||||
List<NodeRef> nodeRefs = new ArrayList<NodeRef>(length);
|
||||
try
|
||||
{
|
||||
for (ResultSetRow row : rs)
|
||||
{
|
||||
nodeRefs.add(row.getNodeRef());
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
rs.close();
|
||||
}
|
||||
// done
|
||||
return nodeRefs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #move(NodeRef, NodeRef, String)
|
||||
@@ -467,10 +540,9 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
targetParentRef = assocRef.getParentRef();
|
||||
}
|
||||
|
||||
boolean checkExists = true;
|
||||
// there is nothing to do if both the name and parent folder haven't changed
|
||||
if (targetParentRef.equals(assocRef.getParentRef()))
|
||||
{
|
||||
// there is nothing to do if both the name and parent folder haven't changed
|
||||
if (newName.equals(beforeFileInfo.getName()))
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
@@ -484,18 +556,9 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
}
|
||||
else if (newName.equalsIgnoreCase(beforeFileInfo.getName()))
|
||||
{
|
||||
// name has only changed case so don't bother with exists check
|
||||
checkExists = false;
|
||||
}
|
||||
}
|
||||
|
||||
// check for existing file or folder (if name has changed)
|
||||
if (checkExists)
|
||||
{
|
||||
checkExists(targetParentRef, newName);
|
||||
}
|
||||
|
||||
|
||||
QName qname = QName.createQName(
|
||||
NamespaceService.CONTENT_MODEL_1_0_URI,
|
||||
QName.createValidLocalName(newName));
|
||||
@@ -536,8 +599,15 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
String currentName = (String)nodeService.getProperty(targetNodeRef, ContentModel.PROP_NAME);
|
||||
if (currentName.equals(newName) == false)
|
||||
{
|
||||
// changed the name property
|
||||
nodeService.setProperty(targetNodeRef, ContentModel.PROP_NAME, newName);
|
||||
try
|
||||
{
|
||||
// changed the name property
|
||||
nodeService.setProperty(targetNodeRef, ContentModel.PROP_NAME, newName);
|
||||
}
|
||||
catch (DuplicateChildNodeNameException e)
|
||||
{
|
||||
throw new FileExistsException(targetParentRef, newName);
|
||||
}
|
||||
}
|
||||
|
||||
// get the details after the operation
|
||||
@@ -581,9 +651,6 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
throw new AlfrescoRuntimeException("The type is not supported by this service: " + typeQName);
|
||||
}
|
||||
|
||||
// check for existing file or folder
|
||||
checkExists(parentNodeRef, name);
|
||||
|
||||
// set up initial properties
|
||||
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(11);
|
||||
properties.put(ContentModel.PROP_NAME, (Serializable) name);
|
||||
@@ -599,12 +666,21 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
QName qname = QName.createQName(
|
||||
NamespaceService.CONTENT_MODEL_1_0_URI,
|
||||
QName.createValidLocalName(name));
|
||||
ChildAssociationRef assocRef = nodeService.createNode(
|
||||
parentNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
qname,
|
||||
typeQName,
|
||||
properties);
|
||||
ChildAssociationRef assocRef = null;
|
||||
try
|
||||
{
|
||||
assocRef = nodeService.createNode(
|
||||
parentNodeRef,
|
||||
ContentModel.ASSOC_CONTAINS,
|
||||
qname,
|
||||
typeQName,
|
||||
properties);
|
||||
}
|
||||
catch (DuplicateChildNodeNameException e)
|
||||
{
|
||||
throw new FileExistsException(parentNodeRef, name);
|
||||
}
|
||||
|
||||
NodeRef nodeRef = assocRef.getChildRef();
|
||||
FileInfo fileInfo = toFileInfo(nodeRef);
|
||||
// done
|
||||
@@ -639,31 +715,25 @@ public class FileFolderServiceImpl implements FileFolderService
|
||||
|
||||
NodeRef currentParentRef = parentNodeRef;
|
||||
// just loop and create if necessary
|
||||
FileInfo lastFileInfo = null;
|
||||
for (String pathElement : pathElements)
|
||||
{
|
||||
try
|
||||
// does it exist?
|
||||
NodeRef nodeRef = searchSimple(currentParentRef, pathElement);
|
||||
if (nodeRef == null)
|
||||
{
|
||||
// not present - make it
|
||||
FileInfo createdFileInfo = create(currentParentRef, pathElement, folderTypeQName);
|
||||
currentParentRef = createdFileInfo.getNodeRef();
|
||||
lastFileInfo = createdFileInfo;
|
||||
}
|
||||
catch (FileExistsException e)
|
||||
else
|
||||
{
|
||||
// it exists - just get it
|
||||
List<FileInfo> fileInfos = search(currentParentRef, pathElement, false, true, false);
|
||||
if (fileInfos.size() == 0)
|
||||
{
|
||||
// ? It must have been removed
|
||||
throw new AlfrescoRuntimeException("Path element has just been removed: " + pathElement);
|
||||
}
|
||||
currentParentRef = fileInfos.get(0).getNodeRef();
|
||||
lastFileInfo = fileInfos.get(0);
|
||||
// it exists
|
||||
currentParentRef = nodeRef;
|
||||
}
|
||||
}
|
||||
// done
|
||||
return lastFileInfo;
|
||||
FileInfo fileInfo = toFileInfo(currentParentRef);
|
||||
return fileInfo;
|
||||
}
|
||||
|
||||
public List<FileInfo> getNamePath(NodeRef rootNodeRef, NodeRef nodeRef) throws FileNotFoundException
|
||||
|
Reference in New Issue
Block a user