alfresco-community-repo/source/java/org/alfresco/repo/security/person/SplitPersonCleanupBootstrapBean.java
Gavin Cornwell af5fd511b7 Merged V2.2 to HEAD
7650: Merged V2.1 to V2.2
     7542: ML contribution fixes
     7545: Merged V2.0 to V2.1
        7544: Merged V1.4 to V2.0
           7338: Fix for potential time-based GUID issues
     7554: Fixed imports after library package change
     7582: WCM-967
     7584: Fixed dependecy list for SDK for jug-lgpl-2.0.0.jar
     7642: Fix for WCM-949

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@8455 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2008-03-07 10:32:22 +00:00

180 lines
6.0 KiB
Java

/*
* 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.security.person;
import java.util.Set;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.AbstractLifecycleBean;
import org.alfresco.util.GUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.safehaus.uuid.UUID;
import org.springframework.context.ApplicationEvent;
/**
* Remove all duplicate users that have previously been split and had guids added to the uid. This been should be wired up into a custom bootstrap process
*
* @author Andy Hind
*/
public class SplitPersonCleanupBootstrapBean extends AbstractLifecycleBean
{
protected final static Log log = LogFactory.getLog(SplitPersonCleanupBootstrapBean.class);
private NodeService nodeService;
private PersonService personService;
private TransactionService transactionService;
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setPersonService(PersonService personService)
{
this.personService = personService;
}
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
@Override
protected void onBootstrap(ApplicationEvent event)
{
// reindex
log.info("Removing person entries with GUIDS at the end of the uid ...");
int count = removePeopleWithGUIDBasedIds();
log.info("... removed " + count);
}
/**
* Can have uid+GUID or uid + "(" + GUID + ")"
*
* @return
*/
private int removePeopleWithGUIDBasedIds()
{
Integer count = transactionService.getRetryingTransactionHelper().doInTransaction(
new RetryingTransactionCallback<Integer>()
{
public Integer execute() throws Exception
{
int count = 0;
// A GUID should be 36 chars
Set<NodeRef> people = personService.getAllPeople();
for (NodeRef person : people)
{
String uid = DefaultTypeConverter.INSTANCE.convert(String.class, nodeService.getProperty(
person, ContentModel.PROP_USERNAME));
if (isUIDWithGUID(uid))
{
// Delete via the person service to get the correct tidy up
personService.deletePerson(uid);
if (log.isDebugEnabled())
{
log.debug("... removed person with uid " + uid);
}
log.info("... removed person with uid " + uid);
count++;
}
}
return count;
}
});
return count.intValue();
}
private boolean isUIDWithGUID(String uid)
{
if (uid.length() > 36)
{
// uid + GUID
// Check the last 36 chars are a valid guid
String guidString = uid.substring(uid.length() - 36);
try
{
@SuppressWarnings("unused")
UUID id = new UUID(guidString);
// We have a valid guid.
return true;
}
catch (NumberFormatException e)
{
// Not a valid GUID
}
}
if (uid.length() > 38)
{
// UID + "(" + GUID + ")"
String guidString = uid.substring(uid.length() - 38);
if (guidString.startsWith("(") && guidString.endsWith(")"))
{
guidString = guidString.substring(1, 37);
try
{
@SuppressWarnings("unused")
UUID id = new UUID(guidString);
// We have a valid guid.
return true;
}
catch (NumberFormatException e)
{
// Not a valid GUID
}
}
}
return false;
}
@Override
protected void onShutdown(ApplicationEvent event)
{
// Nothing to do
}
public static void main(String[] args)
{
SplitPersonCleanupBootstrapBean tester = new SplitPersonCleanupBootstrapBean();
String[] test = new String[] { "andy", "andy" + GUID.generate(), "andy(" + GUID.generate() + ")",
GUID.generate() + "banana", "andy" + GUID.generate() + "banana",
"adbadbaddbadbadbadbabdbadbadbabdabdbbadbadbabdbadbadbadb"
};
for (String uid : test)
{
System.out.println(uid + " ... is a uid with guid = " + tester.isUIDWithGUID(uid));
}
}
}