AnsweredAssumed Answered

Getting current application context

Question asked by p3d3r0s0 on Jun 4, 2010
Latest reply on Aug 30, 2011 by wass
Hey, i was trying to update an sql table i made using hibernate, but i cant seem to get the current ApplicationContext.
Im trying to get the ApplicationContext inside the DbNodeServiceImpl class so that everytime a node is created i can update my counter in the table.

I was trying:

      ApplicationContext ac = ApplicationContextHelper.getApplicationContext();
      TypeHome th = (TypeHome)ac.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);

but this so far only leads to:
"Failed to create content due to error: A valid SecureContext was not provided in the RequestContext"

Can anyone help?
Regards


Edit: In my case this was solved using:
protected static ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());

and i used to get a row(that i called Type) from my table(named TypeHome):

   public static Type getMyType(String typeQName) {
      TypeHome th = (TypeHome)ctx.getBean("TypeHome");
      Type currentType = th.findByQname(typeQName);
      return currentType;
   }

and finally, in my class TypeHome i used this method to get a row or Type that column name that matched the string qname:

   @SuppressWarnings("unchecked")
   public Type findByQname(String qname) {
      log.debug("finding Type instance by example");
      try {
         List<Type> results = (List<Type>) getSession().createCriteria(
                     "com.ptsi.pedroStuff.Type").add(Restrictions.like("name", qname).ignoreCase())
               .list();
         log.debug("find by example successful, result size: "
               + results.size());
         return results.get(0);
      } catch (RuntimeException re) {
         log.error("find by example failed", re);
         throw re;
      }
   }

since the name column is unique i only need the first result (hence the results.get(0);)

hope this helps someone.

Outcomes