cancel
Showing results for 
Search instead for 
Did you mean: 

override methode

litllel
Champ in-the-making
Champ in-the-making
Hi guys, how can I override this methode
  void homeFolderCreateAndSetPermissions(HomeFolderProvider2 provider, NodeRef personNodeRef) 
for my alfresco, this method is in
 public class PortableHomeFolderManager implements HomeFolderManager 
4 REPLIES 4

steven_okennedy
Star Contributor
Star Contributor
Hi,

First of all, bear in mind that any override of the out of the box elements has a cost in maintainability and sustainability - no matter how cleanly you do the override.  That said, the cleanest way to go about this will be to override the Spring Bean that Alfresco uses to instantiate this class.  So copy the below into your own custom context


<bean name="homeFolderManager" class="org.alfresco.repo.security.person.PortableHomeFolderManager">
<property name="nodeService">
<ref bean="NodeService"/>
</property>
<property name="defaultProvider">
<ref bean="userHomesHomeFolderProvider"/>
</property>
<property name="fileFolderService">
<ref bean="FileFolderService"/>
</property>
<property name="searchService">
<ref bean="SearchService"/>
</property>
<property name="NamespaceService">
<ref bean="NamespaceService"/>
</property>
<property name="singletonCache">
<ref bean="immutableSingletonCache"/>
</property>
</bean>


and then replace the class name with your own custom class.  E.g.


<bean name="homeFolderManager" class="org.mycode.MyCustomHomeFolderManager">
<property name="nodeService">
<ref bean="NodeService"/>
</property>
<property name="defaultProvider">
<ref bean="userHomesHomeFolderProvider"/>
</property>
<property name="fileFolderService">
<ref bean="FileFolderService"/>
</property>
<property name="searchService">
<ref bean="SearchService"/>
</property>
<property name="NamespaceService">
<ref bean="NamespaceService"/>
</property>
<property name="singletonCache">
<ref bean="immutableSingletonCache"/>
</property>
</bean>


In your custom class, extend the out of the box org.alfresco.repo.security.person.PortableHomeFolderManager class (you'll need to include alfresco-repository as a dependency in your amp project to get things to compile - scoped as "provided" so you don't include the classes twice) and just include the methods you want to override.  Since the method above looks like it's package scoped so you'll need to include your custom class in the org.alfresco.repo.security.person package if you want to override it.

Rather than doing an invasive change in the product, there might be other ways to achieve what you want using standard, maintainable extension points (e.g. policies/behaviours).  What are you looking to achieve?

Regards

Steven

Thanks you for your answer.
I want to fix the homefolder for a same user when I recreate him after a deletion from ldap in order to keep all his documents.

steven_okennedy
Star Contributor
Star Contributor
In that case, it might be simpler to override the PortableHomeFolderManager.getHomeFolder method, rather than homeFolderCreateAndSetPermissions, since getHomeFolder is where it chooses to use an existing folder or create a new one for a new user, appending a number to the end if it exists.

It currently checks to see whether the user has the property cm:homeFolder has been set, if the person has that property it reuses the folder being pointed to.  Otherwise it will create a new folder (appending the -1, -2, … -N as needed).  You could easily change the way this works by checking to see if the user's preferred home folder exists first and if it does set this as the value of cm:homeFolder on the person object, e.g.

<code>
@Override
public HomeSpaceNodeRef getHomeFolder(HomeFolderProvider2 provider, NodeRef person, boolean referenceRootNode)
{
   HomeSpaceNodeRef homeSpaceNodeRef = null;
   try
   {
      NodeRef existingHomeFolder = DefaultTypeConverter.INSTANCE.convert(
         NodeRef.class, nodeService.getProperty(
         person, ContentModel.PROP_HOMEFOLDER));
       
      //Check to see if the user's preferred user home folder exists
      NodeRef root = getRootPathNodeRef(provider);
      List<String> homeFolderPath = provider.getHomeFolderPath(person);
      FileInfo nonMappedExistingHomeFolder = fileFolderService.resolveNamePath(root, homeFolderPath, false);
          
      if (existingHomeFolder == null && nonMappedExistingHomeFolder != null)
      {
         //We found a folder for this user that's not mapped to that user, so
          //set it as the person's homefolder and return the HomeSpaceNodeRef 
         nodeService.setProperty(person, ContentModel.PROP_HOMEFOLDER, nonMappedExistingHomeFolder.getNodeRef());
         homeSpaceNodeRef = new HomeSpaceNodeRef(existingHomeFolder, HomeSpaceNodeRef.Status.VALID);
      }
      else
      {
         //Otherwise do what the base class does
         homeSpaceNodeRef = super.getHomeFolder(provider, person, referenceRootNode);
      }
   }
   catch(FileNotFoundException fnfe)
   {
   //Because we're setting the mustExist argument to false on resolveNamePath
   //this exception should not be thrown, but is thrown as a checked exception so
   //must be caught in any case - do nothing.
   }
   return homeSpaceNodeRef;
}
<code>

Another way that you might be able to achieve the same behaviour, without the invasive override, would be with a Behaviour that runs after the creation of a cmSmiley Tongueerson node and checks to see if that user's user home folder already exists and if so sets the cm:homeFolder at that stage - although there may be some timing issues doing it this way (to do with the way behaviours/transactions work together)

Regards

Steven

Ok thks you for your help, I will try to do this.