AnsweredAssumed Answered

Problem in getting serviceregistry

Question asked by jeyaa on Jan 12, 2011
Latest reply on Jan 13, 2011 by jeyaa
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

Outcomes