cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in getting serviceregistry

jeyaa
Champ in-the-making
Champ in-the-making
Hi all,
I am new to alfresco. Right now am working with quartz scheduler, where the scheduler has to get the registry services offered by alfresco. My scheduler will call the webscript,

importPackage(Packages.com.kaplan.external.webscript.artesia);
importPackage(Packages.org.alfresco.service.cmr.repository);
importPackage(Packages.org.alfresco.repo.node.archive);

var aliasXmlFilename    = null;
var aliasXmlContent    = null;
var tmpXmlContent      = null;
var placeImgFilename    = null;
var placeImgContent    = null;
var space             = null;
var assetId            = null;
var product            = null;

var configXml    = new XML(config.script);
var helper       = new Helper();
var response   = new Status();

for each (field in formdata.fields)
{
  logger.log(field.name + "=" + field.value);
  if (field.name == "space")
  {
     space = field.value;
  }
  else if (field.isFile)
  {
       if (field.name == "imageAlias") {
          aliasXmlContent = field.content;
          tmpXmlContent = field.content;
       }
       else if (field.name == "placementImage")
       {
            placeImgContent = field.content;
       }
  }
}

// ensure mandatory file attributes have been located
if (aliasXmlContent == undefined || placeImgContent == undefined)
{
   response.code = response.INVALID_REQUEST;
   response.description = "Request parameters are invalid either imageAlias or placementImage is empty";
}
else
{ // before that check Schema Validation
   logger.log("– " + configXml.assetIdXpath);
   logger.log("– " + aliasXmlContent.getContent());

   assetId = helper.getXMLElementValue(aliasXmlContent.getContent(), configXml.assetIdXpath).get(0);
   aliasXmlFilename = helper.getXMLElementValue(aliasXmlContent.getContent(), "/alias-xml/@alias-data-file-name").get(0);
   placeImgFilename = helper.getXMLElementValue(aliasXmlContent.getContent(), "/alias-xml/@placement-image-name").get(0);
   product = helper.getXMLElementValue(aliasXmlContent.getContent(), "/alias-xml/@product").get(0);
   logger.log("– " + assetId + " – " + aliasXmlFilename + " – " + placeImgFilename + " – " + product);
   
   // Find the proper path/space location in alfresco
   new RemoveArchivedNodes().remove();
   var props = configXml.spaceProperties.prop;
   if ((space == undefined || space == "") && product == "")
   {
      space = "PATH:\"/app:company_home" + configXml.defaultPath + "\" -ASPECT:\"sys:archived\"";
   }
   else if (product != "")
   {
      for (i=0; i<props.length(); i++)
      {   
         if (product == props[i].key) {
            space = "PATH:\"/app:company_home" + props[i].value + "\" -ASPECT:\"sys:archived\"";
         }
      }
   }
   else
   {
      space = "PATH:\"/app:company_home" + space.replace("/", "/cm:") + "\" -ASPECT:\"sys:archived\"";
   }
   logger.log("– " + space); // Sample: PATH:"/app:company_home/cm:KPUB/cm:Images"   
   
   // Start the search and insert operation
   
   var resultNodes = search.luceneSearch(space);
   logger.log("———-Result Nodes length : " + resultNodes.length);
     if (resultNodes.length != 0)
     {
        var sNode = resultNodes[0];
        try {
           resultNodes = search.luceneSearch("@ak\\:imageId:\""+ assetId +"\" -ASPECT:\"sys:archived\""); //Sample @ak\:imageId:"123-abc-456"
           logger.log("————-Inner result node length" + resultNodes.length);
           [b]new RemoveArchivedNodes().remove(resultNodes);[/b]
           if (resultNodes.length == 0)
           { //Inserting the content
                 logger.log("– INSERTING…. ");
                var imgNode = sNode.createNode(placeImgFilename, "ak:artesiaAsset");
                imgNode.properties.content.write(placeImgContent);
               
               var aliasNode = sNode.createNode(aliasXmlFilename , "ak:aliasMetadata");
               aliasNode.properties.content.write(tmpXmlContent);
               aliasNode.properties.encoding = "UTF-8";
               aliasNode.createAssociation(imgNode, "ak:placementImageNode");
               
               aliasNode.save();
               imgNode.save();   
               
               for each (i in configXml.aliasProperties.prop)
               {
                  helper.addAliasProp(i.key, i.value);
               }
               for each (i in configXml.imageProperties.prop)
               {
                  helper.addImageProp(i.key, i.value);
               }    
               helper.updateNodeProperties(aliasNode, imgNode);
               response.code = response.SUCCESS;
               response.description = "Files are successfully inserted.";
   
   
           }
           else
           { // Updating the content
              logger.log("– UPDATING…. ");
              var aliasNode = resultNodes[0];
              var imgNode = aliasNode.assocs["ak:placementImageNode"][0];
              aliasNode.properties.content.write(tmpXmlContent);
              imgNode.properties.content.write(placeImgContent);
              
              aliasNode.save();   
              imgNode.save();
            
            for each (i in configXml.aliasProperties.prop)
            {
               helper.addAliasProp(i.key, i.value);
            }
            for each (i in configXml.imageProperties.prop)
            {
               helper.addImageProp(i.key, i.value);
            }
            helper.updateNodeProperties(aliasNode, imgNode);            
              response.code = response.SUCCESS;
            response.description = "Files are successfully updated.";      
           }
      } catch (e) {
         logger.log("– ERROR…. " + e);
         if (e.javaException instanceof DuplicateChildNodeNameException) {
            response.code = response.DUPLICATE_FILE_NAME;
         } else {
                response.code = response.GENERAL_EXCEPTION;
          }
          response.description = e.message;
      }
     }
     else
     {
        response.code = response.INVALID_SPACE;
      response.description = "Given Space " + space + " does not exist in Alfresco CMS ";
     }
}
model.status = response;

from the above js file, new RemoveArchivedNodes().remove(resultNodes); will have to purge the archived nodes in workspace://spacesstore

So I have used NodeArchiveService class

public class RemoveArchivedNodes 
{
   public void remove()
   {
      NodeArchiveService nas = new NodeArchiveServiceImpl();
                // How to get the node Archive service
      StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
      if(storeRef != null)
      {
         //nas.purgeAllArchivedNodes(storeRef);
         System.out.println("Purged All Archived nodes");
      }
      System.out.println("————– Out of the Class ——————");
   }
}

In the above code I need to set the instance of NodeArchiveService. For that I must have ServiceRegistry. So I tried with
ApplicationContext appContext = ApplicationContextHelper.getApplicationContext();
ServiceRegistry registry = (ServiceRegistry) appContext.getBean("serviceRegistry");

But It is not working well. After I added the above lines of code my server is getting restarted. So how could I get the Service registry out here?. Or Is there any other option for my problem?  Please help me in this…


Thanks,
Jeya
2 REPLIES 2

patil
Champ on-the-rise
Champ on-the-rise
Hi Jeyaa,

Use the below configuration and the class

   
   <bean id="exportInvoices" class="org.alfresco.util.CronTriggerBean">
      <property name="jobDetail">
         <ref bean="exportInvoicesPSAPAM" />
      </property>
      <property name="scheduler">
         <ref bean="schedulerFactory" />
      </property>
      <property name="cronExpression">
         <value>0 0/5 * * * ? </value>
      </property>
   </bean>
   
   <bean id="exportInvoicesPSAPAM" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass">
         <value>com.suth.blm.jobs.ExportInvoicesPSAPAM</value>
      </property>
      <property name="jobDataAsMap">
         <map>
            <entry key="ServiceRegistry">
               <ref bean="ServiceRegistry" />
            </entry>
            <entry key="ftpUserName" value="patil" />
            <entry key="ftpUserpassword" value="patil" />
            <entry key="ftpIP" value="127.0.0.1" />
            <entry key="ftpPort" value="40" />   
            <entry key="ftpSourcePath" value="/sutherland" />   
            <entry key="ftpTargetPath" value="sutherland" />      
         </map>
      </property>
   </bean>



package com.suth.blm.jobs;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alfresco.model.ContentModel;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.suth.blm.httpCalls.HttpUtility;

public class ExportInvoicesPSAPAM implements Job {

   /* Class level variables */
   private static Logger LOGGER = Logger.getLogger(ExportInvoicesPSAPAM.class);

   private ServiceRegistry serviceRegistry;

   public ServiceRegistry getServiceRegistry() {
      return serviceRegistry;
   }

   public void setServiceRegistry(ServiceRegistry serviceRegistry) {
      this.serviceRegistry = serviceRegistry;
   }

   @Override
   public void execute(JobExecutionContext jobExecutionContext)
         throws JobExecutionException {
      LOGGER.debug("Trigger the job in the configured time");
      // check for the holiday
            JobDataMap jobDataMap = jobExecutionContext.getJobDetail()
            .getJobDataMap();

      String ftpUserName = (String) jobDataMap.get("ftpUserName");
      String ftpPassword = (String) jobDataMap.get("ftpUserpassword");
      String ftpIP = (String) jobDataMap.get("ftpIP");
      String ftpPort = (String) jobDataMap.get("ftpPort");
      String ftpSourcePath = (String) jobDataMap.get("ftpSourcePath");
      String ftpTargetPath = (String) jobDataMap.get("ftpTargetPath");
      
      
      
      
      

      System.out.println("calling the http method before this we have to get the ticket");      
      HttpUtility.getContent("");
      
      
      serviceRegistry = (ServiceRegistry) jobDataMap.get("ServiceRegistry");

      System.out.println("UserName and the password from the map is :::"
            + ftpUserName + "::Smiley Tongueasswrd:::" + ftpPassword);

      
      // read the files from sourcepath and create in the destination Path
      //performFTPOperation(ftpUserName, ftpPassword, ftpIP, ftpPort);

      //serviceRegistry.getNodeService().createNode(parentRef, assocTypeQName, assocQName, nodeTypeQName);

   }

   

}



Enjoy….



Thanks,
Patil
Cignex Technologies
Bangalore

jeyaa
Champ in-the-making
Champ in-the-making
Thanks Patil,
Will try and let you know.. Thanks once again and Happy Pongal