cancel
Showing results for 
Search instead for 
Did you mean: 

getting faces context instance in the java backed web script

amarendra_thaku
Champ in-the-making
Champ in-the-making
Dear All,

I have written a java backed web script in which i am using the search service

      SearchParameters sp = new SearchParameters();
           sp.addStore(Repository.getStoreRef());
           sp.setLanguage(SearchService.LANGUAGE_LUCENE);
           sp.setQuery(qryString.toString());


From the resutl set i am getting the node list of nodes

e.g.

results = searchService.query(sp);
if(results!=null && results.length()>0)
{
for(ResultSetRow row : results)
{
NodeRef currentNodeRef = row.getNodeRef();
alfNode = new Node(currentNodeRef);
}
}


while accessing the properties of the node (alfNode.getProperties()) i am getting the exception "java.lang.IllegalArgumentException: FacesContext must not be null"

the complete stack trace is


java.lang.IllegalArgumentException: FacesContext must not be null
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContextUtils.java:50)
        at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:81)
        at org.alfresco.web.bean.repository.Repository.getServiceRegistry(Repository.java:593)
        at org.alfresco.web.bean.repository.Repository.getUserTransaction(Repository.java:579)
        at com.undp.se.search.SearchResourceAction.searchResource(SearchResourceAction.java:84)
        at com.undp.se.action.SearchAction.executeImpl(SearchAction.java:45)
        at org.alfresco.web.scripts.DeclarativeWebScript.executeImpl(DeclarativeWebScript.java:209)
        at org.alfresco.web.scripts.DeclarativeWebScript.executeImpl(DeclarativeWebScript.java:223)
        at org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:70)
        at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:358)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:408)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:425)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:289)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)
        at org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Unknown Source)


I have to login to the alfresco application once in order to avoid this exception.

I am also initializing the resquired services in the "web-scripts-application-context.xml" e.g.

   <bean id="webscript.com.abc.xyz.search.search.post"
         class="com.abc.xyz.action.SearchAction"
         parent="webscript">
         <property name="searchService" ref="SearchService" />
         <property name="nodeService" ref="nodeService" />
         <property name="dictionaryService" ref="dictionaryService" />        
   </bean>


I am using the alfresco 3.2 community edition

Unable to find what could be problem.

Required suggestion.

Thanks & Regards

Amarendra Thakur
7 REPLIES 7

amarendra_thaku
Champ in-the-making
Champ in-the-making
I understand that that the Java backed web script is outside the JSF lifecycle when it is called by the any 3rd party tool.
So i just wanted to know what could be the other workaround for the same

rogier_oudshoor
Champ in-the-making
Champ in-the-making
You should use the nodeService to retrieve the properties from the node using the references you get from the searchservice. The nodeService has no references to the FacesContext (at least, that i know of) …

amarendra_thaku
Champ in-the-making
Champ in-the-making
Thanks rogier.oudshoorn,

You should use the nodeService to retrieve the properties from the node using the references you get from the searchservice. The nodeService has no references to the FacesContext (at least, that i know of) …


My thought is that once i get the node object i will put that in the model and then i will iterate that node object in the ftl to get its property

alfNode = new Node(currentNodeRef);
resultList.add(alfNode);


and then
Map<String, Object> model = null;
model.put("node", resultList);



In this case i did`nt find any api to get the node object from nodeService
so while iterating the node object in the ftl i am geting the error "java.lang.IllegalArgumentException: FacesContext must not be null".

my ftl is like this

<#if node?exists>
   {
      "total":"${total}",
      "type":"${type}",
      "result":[
      <#list node as n>
      {"Id":"${n.properties["km:Id"]}",
         "Title":"${n.properties["km:Title"]}",
         "Type":"${n.properties["km:Type"]}"}
      <#if n_has_next>,</#if>
      </#list>
      ]
   }
<#else>
   {
      "total":"0",
      "type":"${type}",
      "result":""
   }
</#if>



The complete stack trace is


org.alfresco.web.scripts.WebScriptException: 03300012 Wrapped Exception (with status template): 03300011 Error during processing of the template 'get(properties) failed on instance of org.alfresco.web.bean.repository.Node'. Please contact your system
administrator.
        at org.alfresco.web.scripts.AbstractWebScript.createStatusException(AbstractWebScript.java:613)
        at org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:165)
        at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:358)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:408)
        at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:425)
        at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:289)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)
        at org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)
        at org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
        at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
        at java.lang.Thread.run(Unknown Source)
Caused by: org.alfresco.service.cmr.repository.TemplateException: 03300011 Error during processing of the template 'get(properties) failed on instance of org.alfresco.web.bean.repository.Node'. Please contact your system administrator.
        at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:205)
        at org.alfresco.web.scripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:523)
        at org.alfresco.web.scripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:241)
        at org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
        … 21 more
Caused by: freemarker.template.TemplateModelException: get(properties) failed on instance of org.alfresco.web.bean.repository.Node
        at freemarker.ext.beans.BeanModel.get(BeanModel.java:223)
        at freemarker.core.Dot._getAsTemplateModel(Dot.java:76)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:74)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Expression.getStringValue(Expression.java:93)
        at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
        at freemarker.core.Environment.visit(Environment.java:415)
        at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.IfBlock.accept(IfBlock.java:82)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.Environment.process(Environment.java:188)
        at freemarker.template.Template.process(Template.java:237)
        at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:201)
        … 24 more
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:835)
        at freemarker.ext.beans.BeanModel.invokeThroughDescriptor(BeanModel.java:276)
        at freemarker.ext.beans.BeanModel.get(BeanModel.java:183)
        … 46 more
Caused by: java.lang.IllegalArgumentException: FacesContext must not be null
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContextUtils.java:50)
        at org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:81)
        at org.alfresco.web.bean.repository.Repository.getServiceRegistry(Repository.java:593)
        at org.alfresco.web.bean.repository.Node.getServiceRegistry(Node.java:520)
        at org.alfresco.web.bean.repository.Node.getProperties(Node.java:109)

amarendra_thaku
Champ in-the-making
Champ in-the-making
Any Ideas about this issue?.Its very urgent as i can not proceed without this.

Thanks & Regards
Amarendra Thakur

openpj
Elite Collaborator
Elite Collaborator
Can you share with us all your code with all the imports?
I think that you are using a wrong import class in your code.

amarendra_thaku
Champ in-the-making
Champ in-the-making
Dear OpenPJ,

Here is the source code

package com.ia.search;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.bean.repository.Node;
import org.alfresco.web.scripts.WebScriptRequest;


import com.ia.model.ContentModel;
import com.ia.utils.RepoSearchInitializer;
import com.ia.utils.RepoSearchUtils;
import com.ia.utils.RepositoryConstants;
import org.json.JSONArray;

public class SearchResourceAction {
private transient ServiceRegistry serviceRegistry;   
   public Map<String, Object> searchRepository()
   {
      Map<String, Object> model = null;
      Map<String, Object> nodeProperties = null;
      Map<String, NodeRef> nodeRefs = null;
      ArrayList resultList = new ArrayList();
        try
        {
      model = new HashMap<String, Object>();

      String qry = "@\\{com.ia.repository\\}empName:amarendra";

      SearchParameters sp = new SearchParameters();
      sp.addStore(Repository.getStoreRef());
      sp.setLanguage(SearchService.LANGUAGE_LUCENE);
      sp.setQuery(qryString.toString());         

           ResultSet results = null;
           Node alfNode = null;
      
           try
           {
               results = serviceRegistry.getSearchService().query();
               if(results!=null && results.length()>0)
               {
                  for(ResultSetRow row : results)
                  {
                     NodeRef currentNodeRef = row.getNodeRef();
                      if (serviceRegistry.getNodeService().exists(currentNodeRef))
                      {
                          QName type = serviceRegistry.getNodeService().getType(currentNodeRef);

                          TypeDefinition typeDef = serviceRegistry.getDictionaryService().getType(type);
                          if (typeDef != null)
                          {
                              if (serviceRegistry.getDictionaryService().isSubClass(type, ContentModel.TYPE_RESOURCE))
                              {
                  alfNode = new Node(currentNodeRef);
                  resultList.add(alfNode);
                              }                             
                          }                         
                      }
                  }
         model.put("node", resultList);
         model.put("total", String.valueOf(resultList.size()));
               }
               else
               {
               }
           }
           catch(Exception e1)
           {
              e1.printStackTrace();
           }
           finally
           {
               if(results != null)
               {
                   results.close();
                   //tx.commit();
               }
           }
         
      }
      catch (Exception e) {
         e.printStackTrace();
      }
        return model;
   }

   public ServiceRegistry getServiceRegistry() {
      return serviceRegistry;
   }

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

and in the ftl i am ietrating the model like,


<#if node?exists>
{
"total":"${total}",
"result":[
<#list node as n>
{"Id":"${n.properties["km:Id"]}",
"Title":"${n.properties["km:Title"]}",
"Type":"${n.properties["km:Type"]}"}
<#if n_has_next>,</#if>
</#list>
]
}
<#else>
{
"total":"0",
"result":""
}
</#if>

also in the ftl when i say n.assocs["ia:userSpaceAssoc"].it is giving me the error like "n.assocs" is undefined.

I am not able to figure out how to get the node object without the faces context instance.

Urgent help is required as i am running out of time.

Thanks & Regards

Amarendra Thakur

amarendra_thaku
Champ in-the-making
Champ in-the-making
Finally, i resolved this issue.

What i did was

NodeRef currentNodeRef = row.getNodeRef();
resultList.add(currentNodeRef);
String resultJSON = searchUtils.buildResult(resultList);
resultJsonObj.put("resObj", resultJSON);
resourceJSONArr.put(resultJsonObj);
model.put("resultNodeRef", resourceJSONArr.toString());

and finally into the js which empty by now i did

<import resource="classpath:alfresco/templates/webscripts/com/xyz/json.js">
var nodeRefList = resultNodeRef;
function main()
{
   //var nodeRefList = resultNodeRef;
   //model.resultNodeRef = resultNodeRef;
   var resultList = null;
   var finalResultList = new Array();
   if(resultNodeRef!="[]")
   {
      var resultJSONArr = json.parse(resultNodeRef);   
      for(var i=0;i<resultJSONArr.length;i++)
      {
         var resultsStr = resultJSONArr[i].resObj;
         model.resJson = resultsStr;
         resultList = resultsStr.split(",");
      }
      if(resultList!=null)
      {
         for(var i=0;i<resultList.length;i++)
         {
            var resultNode = search.findNode(resultList[i]);
            finalResultList.push(resultNode);
         }
      }
      model.results = finalResultList;
   }
   else
   {
      //model.results = resultList;
   }
}

main();

Thanks & Regards
Amarendra Thakur