Merged DEV/CHECK_EXISTS to HEAD

svn merge svn://www.alfresco.org:3691/alfresco/BRANCHES/DEV/CHECK_EXISTS@3442 svn://www.alfresco.org:3691/alfresco/BRANCHES/DEV/CHECK_EXISTS@3590 .

TODO: Fix bug raising incorrect exception during UI paste of same-named file
Note:
- Added a new method to NodeService to get child by name
- Added a new method to FileFolderService to perform fast, direct lookups based on name


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@3591 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2006-08-23 16:50:12 +00:00
parent 3c9f179571
commit 4e4e342409
39 changed files with 1577 additions and 637 deletions

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.
*/
package org.alfresco.repo.admin.patch.impl;
import org.alfresco.repo.admin.patch.AbstractPatch;
import org.alfresco.service.cmr.admin.PatchException;
/**
* This patch ensures that an upgrade script has been executed. Upgrade scripts
* should create an entry for the patch with the required ID and execution status
* so that the code in this class is never called. If called, an exception message
* is always generated.
*
* @author Derek Hulley
*/
public class SchemaUpgradeScriptPatch extends AbstractPatch
{
private static final String MSG_NOT_EXECUTED = "patch.schemaUpgradeScript.err.not_executed";
private String scriptName;
public SchemaUpgradeScriptPatch()
{
}
/**
* Set the name of the upgrade script to execute.
*
* @param scriptName the script filename
*/
public void setScriptName(String scriptName)
{
this.scriptName = scriptName;
}
protected void checkProperties()
{
super.checkProperties();
checkPropertyNotNull(scriptName, "scriptName");
}
/**
* @see #MSG_NOT_EXECUTED
*/
@Override
protected String applyInternal() throws Exception
{
throw new PatchException(MSG_NOT_EXECUTED, scriptName);
}
}

View File

@@ -0,0 +1,268 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* 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.
*/
package org.alfresco.repo.admin.patch.impl;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;
import java.util.List;
import org.alfresco.i18n.I18NUtil;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.admin.patch.AbstractPatch;
import org.alfresco.repo.domain.ChildAssoc;
import org.alfresco.repo.domain.Node;
import org.alfresco.repo.node.db.NodeDaoService;
import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.namespace.QName;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Checks that all child node names are unique for the associations that require it.
*
* @author Derek Hulley
*/
public class UniqueChildNamePatch extends AbstractPatch
{
private static final String MSG_SUCCESS = "patch.uniqueChildName.result";
private static final String MSG_COPY_OF = "patch.uniqueChildName.copyOf";
/** the number of associations to process at a time */
private static final int MAX_RESULTS = 1000;
private SessionFactory sessionFactory;
private DictionaryService dictionaryService;
private NodeDaoService nodeDaoService;
public UniqueChildNamePatch()
{
}
public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
/**
* @param dictionaryService The service used to sort out the associations
* that require duplicate checks
*/
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}
/**
* @param nodeDaoService The service that generates the CRC values
*/
public void setNodeDaoService(NodeDaoService nodeDaoService)
{
this.nodeDaoService = nodeDaoService;
}
@Override
protected void checkProperties()
{
super.checkProperties();
checkPropertyNotNull(sessionFactory, "sessionFactory");
checkPropertyNotNull(dictionaryService, "dictionaryService");
checkPropertyNotNull(nodeDaoService, "nodeDaoService");
}
@Override
protected String applyInternal() throws Exception
{
// initialise the helper
HibernateHelper helper = new HibernateHelper();
helper.setSessionFactory(sessionFactory);
String msg = helper.assignCrc();
// done
return msg;
}
private class HibernateHelper extends HibernateDaoSupport
{
private File logFile;
private FileChannel channel;
private HibernateHelper() throws IOException
{
logFile = new File("./UniqueChildNamePatch.log");
// open the file for appending
RandomAccessFile outputFile = new RandomAccessFile(logFile, "rw");
channel = outputFile.getChannel();
// move to the end of the file
channel.position(channel.size());
// add a newline and it's ready
writeLine("").writeLine("");
writeLine("UniqueChildNamePatch executing on " + new Date());
}
private HibernateHelper write(Object obj) throws IOException
{
channel.write(ByteBuffer.wrap(obj.toString().getBytes()));
return this;
}
private HibernateHelper writeLine(Object obj) throws IOException
{
write(obj);
write("\n");
return this;
}
public String assignCrc() throws Exception
{
// get the association types to check
@SuppressWarnings("unused")
List<QName> assocTypeQNames = getUsedAssocQNames();
int fixed = 0;
int processed = 0;
// check loop through all associations, looking for duplicates
for (QName assocTypeQName : assocTypeQNames)
{
AssociationDefinition assocDef = dictionaryService.getAssociation(assocTypeQName);
if (!(assocDef instanceof ChildAssociationDefinition))
{
String msg = "WARNING: Non-child association used to link a child node: " + assocTypeQName;
writeLine(msg);
logger.warn(msg);
continue;
}
ChildAssociationDefinition childAssocDef = (ChildAssociationDefinition) assocDef;
if (childAssocDef.getDuplicateChildNamesAllowed())
{
continue;
}
write("Checking for name duplicates on association type ").writeLine(assocTypeQName);
// get all child associations until there are no more results
long lastAssocId = Long.MIN_VALUE;
int lastResultCount = 1;
while(lastResultCount > 0)
{
writeLine(String.format("...Processed %7d associations with %3d duplicates found...", processed, fixed));
List<Object[]> results = getAssociations(assocTypeQName, lastAssocId) ;
lastResultCount = results.size();
for (Object[] objects : results)
{
ChildAssoc childAssoc = (ChildAssoc) objects[0];
Node childNode = (Node) objects[1];
NodeRef childNodeRef = childNode.getNodeRef();
// get the current name
String childName = (String) nodeService.getProperty(childNodeRef, ContentModel.PROP_NAME);
lastAssocId = childAssoc.getId();
String usedChildName = childName;
processed++;
boolean duplicate = false;
while(true)
{
try
{
// push the name back to the node
nodeService.setProperty(childNodeRef, ContentModel.PROP_NAME, usedChildName);
break; // no issues - no duplicate
}
catch (DuplicateChildNodeNameException e)
{
// there was a duplicate, so adjust the name and change the node property
duplicate = true;
// assign a new name
usedChildName = childName + I18NUtil.getMessage(MSG_COPY_OF, processed);
// try again
}
}
// if duplicated, report it
if (duplicate)
{
fixed++;
// get the node path
NodeRef parentNodeRef = childAssoc.getParent().getNodeRef();
Path path = nodeService.getPath(parentNodeRef);
writeLine(" Changed duplicated child name:");
writeLine(" Parent: " + parentNodeRef);
writeLine(" Parent path: " + path);
writeLine(" Duplicate name: " + childName);
writeLine(" Replaced with: " + usedChildName);
}
}
}
}
// build the result message
String msg = I18NUtil.getMessage(MSG_SUCCESS, processed, fixed, logFile);
return msg;
}
@SuppressWarnings("unchecked")
private List<QName> getUsedAssocQNames()
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session
.createQuery(
"select distinct assoc.typeQName " +
"from org.alfresco.repo.domain.hibernate.ChildAssocImpl as assoc");
return query.list();
}
};
List<QName> results = (List<QName>) getHibernateTemplate().execute(callback);
return results;
}
/**
* @return Returns a list of <tt>ChildAssoc</tt> and <tt>String</tt> instances
*/
@SuppressWarnings("unchecked")
private List<Object[]> getAssociations(final QName assocTypeQName, final long lastAssocId)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
Query query = session
.getNamedQuery("node.patch.GetAssocsAndChildNames")
.setLong("lastAssocId", lastAssocId)
.setParameter("assocTypeQName", assocTypeQName)
.setMaxResults(MAX_RESULTS);
return query.list();
}
};
List<Object[]> results = (List<Object[]>) getHibernateTemplate().execute(callback);
return results;
}
}
}