mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Merge 1.4 to HEAD
svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4340 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4350 . svn resolved root\projects\3rd-party\.classpath svn resolved root\projects\repository\source\java\org\alfresco\repo\workflow\WorkflowInterpreter.java svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4379 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4380 . svn merge svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4420 svn://svn.alfresco.com:3691/alfresco/BRANCHES/V1.4@4421 . svn resolved root\projects\3rd-party\.classpath git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4655 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* 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.hibernate.NodeImpl;
|
||||
import org.alfresco.repo.node.db.NodeDaoService;
|
||||
import org.alfresco.service.cmr.admin.PatchException;
|
||||
import org.alfresco.service.cmr.repository.DuplicateChildNodeNameException;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
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 names do not end with ' ' or '.'
|
||||
*
|
||||
* @author David Caruana
|
||||
*/
|
||||
public class InvalidNameEndingPatch extends AbstractPatch
|
||||
{
|
||||
private static final String MSG_SUCCESS = "patch.invalidNameEnding.result";
|
||||
private static final String MSG_REWRITTEN = "patch.invalidNameEnding.rewritten";
|
||||
private static final String ERR_UNABLE_TO_FIX = "patch.invalidNameEnding.err.unable_to_fix";
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
private NodeDaoService nodeDaoService;
|
||||
|
||||
public InvalidNameEndingPatch()
|
||||
{
|
||||
}
|
||||
|
||||
public void setSessionFactory(SessionFactory sessionFactory)
|
||||
{
|
||||
this.sessionFactory = sessionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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(nodeDaoService, "nodeDaoService");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String applyInternal() throws Exception
|
||||
{
|
||||
// initialise the helper
|
||||
HibernateHelper helper = new HibernateHelper();
|
||||
helper.setSessionFactory(sessionFactory);
|
||||
|
||||
try
|
||||
{
|
||||
String msg = helper.fixNames();
|
||||
// done
|
||||
return msg;
|
||||
}
|
||||
finally
|
||||
{
|
||||
helper.closeWriter();
|
||||
}
|
||||
}
|
||||
|
||||
private class HibernateHelper extends HibernateDaoSupport
|
||||
{
|
||||
private File logFile;
|
||||
private FileChannel channel;
|
||||
|
||||
private HibernateHelper() throws IOException
|
||||
{
|
||||
logFile = new File("./InvalidNameEndingPatch.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("InvalidNameEndingPatch 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;
|
||||
}
|
||||
private void closeWriter()
|
||||
{
|
||||
try { channel.close(); } catch (Throwable e) {}
|
||||
}
|
||||
|
||||
public String fixNames() throws Exception
|
||||
{
|
||||
// get the association types to check
|
||||
@SuppressWarnings("unused")
|
||||
List<NodeImpl> nodes = getInvalidNames();
|
||||
|
||||
int updated = 0;
|
||||
for (NodeImpl node : nodes)
|
||||
{
|
||||
NodeRef nodeRef = node.getNodeRef();
|
||||
String name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
|
||||
if (name != null && (name.endsWith(".") || name.endsWith(" ")))
|
||||
{
|
||||
int i = (name.length() == 0) ? 0 : name.length() - 1;
|
||||
while (i >= 0 && (name.charAt(i) == '.' || name.charAt(i) == ' '))
|
||||
{
|
||||
i--;
|
||||
}
|
||||
|
||||
String updatedName = name.substring(0, i);
|
||||
int idx = 0;
|
||||
boolean applied = false;
|
||||
while (!applied)
|
||||
{
|
||||
try
|
||||
{
|
||||
nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, updatedName);
|
||||
applied = true;
|
||||
}
|
||||
catch(DuplicateChildNodeNameException e)
|
||||
{
|
||||
idx++;
|
||||
if (idx > 10)
|
||||
{
|
||||
writeLine(I18NUtil.getMessage(ERR_UNABLE_TO_FIX, name ,updatedName));
|
||||
throw new PatchException(ERR_UNABLE_TO_FIX, logFile);
|
||||
}
|
||||
updatedName += "_" + idx;
|
||||
}
|
||||
}
|
||||
writeLine(I18NUtil.getMessage(MSG_REWRITTEN, name ,updatedName));
|
||||
updated++;
|
||||
getSession().flush();
|
||||
getSession().clear();
|
||||
}
|
||||
}
|
||||
|
||||
String msg = I18NUtil.getMessage(MSG_SUCCESS, updated, logFile);
|
||||
return msg;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<NodeImpl> getInvalidNames()
|
||||
{
|
||||
HibernateCallback callback = new HibernateCallback()
|
||||
{
|
||||
public Object doInHibernate(Session session)
|
||||
{
|
||||
Query query = session
|
||||
.createQuery(
|
||||
"select node from org.alfresco.repo.domain.hibernate.NodeImpl as node " +
|
||||
"join node.properties prop where " +
|
||||
" prop.stringValue like '%.' or " +
|
||||
" prop.stringValue like '% ' ");
|
||||
return query.list();
|
||||
}
|
||||
};
|
||||
List<NodeImpl> results = (List<NodeImpl>) getHibernateTemplate().execute(callback);
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -104,10 +104,16 @@ public class UniqueChildNamePatch extends AbstractPatch
|
||||
HibernateHelper helper = new HibernateHelper();
|
||||
helper.setSessionFactory(sessionFactory);
|
||||
|
||||
String msg = helper.assignCrc();
|
||||
|
||||
// done
|
||||
return msg;
|
||||
try
|
||||
{
|
||||
String msg = helper.assignCrc();
|
||||
// done
|
||||
return msg;
|
||||
}
|
||||
finally
|
||||
{
|
||||
helper.closeWriter();
|
||||
}
|
||||
}
|
||||
|
||||
private class HibernateHelper extends HibernateDaoSupport
|
||||
@@ -139,6 +145,10 @@ public class UniqueChildNamePatch extends AbstractPatch
|
||||
write("\n");
|
||||
return this;
|
||||
}
|
||||
private void closeWriter()
|
||||
{
|
||||
try { channel.close(); } catch (Throwable e) {}
|
||||
}
|
||||
|
||||
public String assignCrc() throws Exception
|
||||
{
|
||||
|
Reference in New Issue
Block a user