0

I have a scheduler job configured and created an action class by extending org.quartz.StatefulJob

In execute method of Action Class (Shown below) , What would be the best way to get reference to CompanyHome in execute method ?

My objctive to create a file in company home directory , when the action invoke. Any suggesion ?

enter image description here

  • 2
    Maybe [follow a similar approach to how the Tags update scheduled job](https://github.com/Alfresco/alfresco-repository/blob/ac38ac94ff4f9cbdf2671a9517781bda389a13c4/src/main/java/org/alfresco/repo/tagging/UpdateTagScopesQuartzJob.java) works? – Gagravarr Feb 13 '20 at 13:18

2 Answers2

3

Have you tried using NodeLocatorService? https://docs.alfresco.com/4.0/concepts/node-locator-available.html. For example:

NodeRef companyHomeNodeRef = registry.getNodeLocatorService().getNode(CompanyHomeNodeLocator.NAME, null, null);
Lista
  • 2,186
  • 1
  • 15
  • 18
1

Please implement a method like this

public NodeRef getCompanyHomeNodeReference() {
        NodeRef companyHomeNodeRef = null;
        companyHomeNodeRef = (NodeRef)AuthenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
            public Object doWork() throws Exception {
                StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"));
                ResultSet rs = serviceRegistry.getSearchService().query(storeRef, SearchService.LANGUAGE_XPATH,
                        "/app:company_home"); 
                Object companyHome = null;
                try {
                    if (rs.length() == 0) {
                        LOG.error("Didn't find Company Home ");
                        throw new AlfrescoRuntimeException("Didn't find Company Home");
                    }
                    final NodeRef companyHomeNodeRef1 = rs.getNodeRef(0);
                    if(companyHomeNodeRef1 == null) {
                        LOG.info("Didn't find Company Homes");
                    }else {
                        companyHome = companyHomeNodeRef1;
                    }
                } finally {
                    rs.close();
                }
                return companyHome;
            }

        });
        return companyHomeNodeRef;
    }

import as below:

import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.repository.StoreRef;

please see how the critical code is placed in AuthenticationUtil (this is very important).

And then use below code to create a file:

fileFolderService.create(companyNodeRef, "yourfilename", ContentModel.TYPE_CONTENT);

Add this bean in service-context.xml

<bean id="yourObj" class="MoveMonthlyDataAction">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry" />
        </property> 
</bean>

and mention in MoveMonthlyDataAction . java as below,

public class MoveMonthlyDataAction {
    ServiceRegistry serviceRegistry;

    public void execute(){
        // your code
    }
    // getter and setter
}

Hope this will help.

madhepurian
  • 271
  • 1
  • 13