alfrescoalfresco-enterprise

CompanyHome reference in java API


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


Solution

  • 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.