cancel
Showing results for 
Search instead for 
Did you mean: 

Properties of a Custom Model

k_warcaba
Champ in-the-making
Champ in-the-making
Hi all,

I would like to create Alfresco Client. I have already write some code and now I get stuck. I would like to create add content mechanism which will be similar to the one implemented in the native Alfresco Client. When I choose custom content model I would like to know what properties this model has - I need this info to show fields which user will have to fill. My idea is to get this information from Alfresco via WebScript. Unfortunately I cannot find any tips on how to access information of the properties of the custom model. I searched for any JavaScript or Java example, information and I couldn't find anything. Can anyone help me with this issue?

Thanks in Advance,
Kamil Warcaba
5 REPLIES 5

brian_robinson
Champ in-the-making
Champ in-the-making
Hi Kamil,

See Jeff Potts' article called "Working with Custom Content Types": http://ecmarchitect.com/alfresco-developer-series.  This should tell you what you need to know.

Thanks,
-Brian

k_warcaba
Champ in-the-making
Champ in-the-making
Thanks for the answer Brian.

Now I have another problem. I have tried to write my own Java WebScript. Here is my code:

package org.alfresco.module.demoscripts;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.faces.context.FacesContext;

import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.bean.repository.Repository;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class SimpleWebScript extends AbstractWebScript
{
    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
       try
       {
            FacesContext context = FacesContext.getCurrentInstance();
           
            DictionaryService dictionaryService = Repository.getServiceRegistry(context).getDictionaryService();
          Collection<QName> properties = dictionaryService.getProperties(QName.createQName("{http://www.someco.com/model/content/1.0}whitepaper"));
          Iterator<QName> iter = properties.iterator();
          // build a json object
          JSONObject obj = new JSONObject(); 
          int i=1;
          while(iter.hasNext()){
              // put some data on it
              obj.put("prop"+i, iter.next());
              i++;
          }
          
          // build a JSON string and send it back
          String jsonString = obj.toString();
          res.getWriter().write(jsonString);
       }
       catch(JSONException e)
       {
          throw new WebScriptException("Unable to serialize JSON");
       }
    }   
}

As you can see it is based on Java-backed Web Scripts Example. The main idea is to find out the properties of custom model. To achieve that I use DictionaryService which needs FacesContext. And here is my problem. When I deploy my WebScript to Alfresco and restart it I can see such errors:

10:20:48,187 INFO  [org.alfresco.config.xml.XMLConfigService$PropertyConfigurer] Loading properties file from class path resource [alfresco/file-servers.properties]
10:20:53,140 ERROR [org.springframework.web.context.ContextLoader] Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.alfresco.module.demoscripts.SimpleWebScript] for bean with name 'webscript.org.alfresco.demo.simple.get' defined in file [C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\module\demoscripts\module-context.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: Bad version number in .class file
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
   at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
   at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
   at org.springframework.util.ClassUtils.forName(ClassUtils.java:201)
   at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:327)
   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1075)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:282)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
   at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:825)
   at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:714)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
   at org.apache.catalina.core.StandardService.start(StandardService.java:448)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
10:20:53,140 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/alfresco]] Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.alfresco.module.demoscripts.SimpleWebScript] for bean with name 'webscript.org.alfresco.demo.simple.get' defined in file [C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\module\demoscripts\module-context.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: Bad version number in .class file
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
   at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
   at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
   at org.springframework.util.ClassUtils.forName(ClassUtils.java:201)
   at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:327)
   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1075)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:282)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
   at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:825)
   at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:714)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
   at org.apache.catalina.core.StandardService.start(StandardService.java:448)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
10:20:53,140 ERROR [org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/alfresco]] Exception sending context initialized event to listener instance of class org.alfresco.web.app.ContextListener
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.alfresco.module.demoscripts.SimpleWebScript] for bean with name 'webscript.org.alfresco.demo.simple.get' defined in file [C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\module\demoscripts\module-context.xml]: problem with class file or dependent class; nested exception is java.lang.UnsupportedClassVersionError: Bad version number in .class file
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file
   at java.lang.ClassLoader.defineClass1(Native Method)
   at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
   at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
   at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1847)
   at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:873)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1326)
   at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1205)
   at org.springframework.util.ClassUtils.forName(ClassUtils.java:201)
   at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:327)
   at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1075)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:282)
   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
   at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:244)
   at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
   at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:49)
   at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3764)
   at org.apache.catalina.core.StandardContext.start(StandardContext.java:4216)
   at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:760)
   at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:740)
   at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:544)
   at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:825)
   at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:714)
   at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:490)
   at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
   at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
   at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:120)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1022)
   at org.apache.catalina.core.StandardHost.start(StandardHost.java:736)
   at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1014)
   at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
   at org.apache.catalina.core.StandardService.start(StandardService.java:448)
   at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
   at org.apache.catalina.startup.Catalina.start(Catalina.java:552)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:295)
   at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:433)
10:20:57,421 INFO  [org.alfresco.web.site.FrameworkHelper] Successfully Initialized Web Framework


I think something is wrong with initializing the Context. I cannot understand why it is wrong. My code is also based on the example which I have found in the svn repository, where context is initialized in the same way,  sample code:

   /**
    * @return Returns a list of content object types to allow the user to select from
    */
   public List<SelectItem> getContentTypes()
   {
      if ((properties.getContentTypes() == null) || (Application.isDynamicConfig(FacesContext.getCurrentInstance())))
      {
         FacesContext context = FacesContext.getCurrentInstance();
        
         DictionaryService dictionaryService = Repository.getServiceRegistry(context).getDictionaryService();
        
         // add the well known cm:content object type by default
         properties.setContentTypes(new ArrayList<SelectItem>(5));
         properties.getContentTypes().add(new SelectItem(ContentModel.TYPE_CONTENT.toString(),
               dictionaryService.getType(ContentModel.TYPE_CONTENT).getTitle()));
        
         // add any configured content sub-types to the list
         List<String> types = getSearchConfig().getContentTypes();
         if (types != null)
         {
            for (String type : types)
            {
               QName idQName = Repository.resolveToQName(type);
               if (idQName != null)
               {
                  TypeDefinition typeDef = dictionaryService.getType(idQName);
                 
                  if (typeDef != null && dictionaryService.isSubClass(typeDef.getName(), ContentModel.TYPE_CONTENT))
                  {
                     // try and get label from the dictionary
                     String label = typeDef.getTitle();
                    
                     // else just use the localname
                     if (label == null)
                     {
                        label = idQName.getLocalName();
                     }
                    
                     properties.getContentTypes().add(new SelectItem(idQName.toString(), label));
                  }
               }
            }
         }
      }
     
      return properties.getContentTypes();
   }


whole file can be found here:
http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/web-client/source/jav...

Can anyone help with this issue. I am using Alfresco Labs 3b and Alfresco Labs SDK 3a.

Thanks in advance.
Kamil

k_warcaba
Champ in-the-making
Champ in-the-making
I have just found the reason why I get those errors. My code was compiled with jdk1.5 and it was run with jdk1.6. That was why I get "java.lang.UnsupportedClassVersionError: Bad version number in .class file" error.

And here is another issue. My Java code one more time:


package org.alfresco.module.demoscripts;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import javax.faces.context.FacesContext;

import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.bean.repository.Repository;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptException;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.json.JSONException;
import org.json.JSONObject;

public class SimpleWebScript extends AbstractWebScript
{
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException
{
try
{
FacesContext context = FacesContext.getCurrentInstance();

DictionaryService dictionaryService = Repository.getServiceRegistry(context).getDictionaryService();
Collection<QName> properties = dictionaryService.getProperties(QName.createQName("{http://www.someco.com/model/content/1.0}whitepaper"));
Iterator<QName> iter = properties.iterator();
// build a json object
JSONObject obj = new JSONObject();
int i=1;
while(iter.hasNext()){
// put some data on it
obj.put("prop"+i, iter.next());
i++;
}

// build a JSON string and send it back
String jsonString = obj.toString();
res.getWriter().write(jsonString);
}
catch(JSONException e)
{
throw new WebScriptException("Unable to serialize JSON");
}
}
}

And errors I receive in a browser when I run my WebScript:

Message:   FacesContext must not be null
   
Exception:   java.lang.IllegalArgumentException - FacesContext must not be null
   
   org.springframework.util.Assert.notNull(Assert.java:112)
   org.springframework.web.jsf.FacesContextUtils.getWebApplicationContext(FacesContextUtils.java:50)
   org.springframework.web.jsf.FacesContextUtils.getRequiredWebApplicationContext(FacesContextUtils.java:81)
   org.alfresco.module.demoscripts.SimpleWebScript.isDynamicConfig(SimpleWebScript.java:63)
   org.alfresco.module.demoscripts.SimpleWebScript.execute(SimpleWebScript.java:28)
   org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:297)
   org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:230)
   org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:240)
   org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)
   org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:116)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   org.alfresco.web.app.servlet.MTWebScriptAuthenticationFilter.doFilter(MTWebScriptAuthenticationFilter.java:102)
   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:870)
   org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
   org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
   org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
   org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
   java.lang.Thread.run(Thread.java:595)

Does anyone know why FacesContext is null?

Thanks for any help.
Kamil

merimm
Champ in-the-making
Champ in-the-making
Hi Kamil,

I have the same problem. Can you tell me how solved it, please?

Thanks in advance.

Regards,

merimm

brian_robinson
Champ in-the-making
Champ in-the-making
Hi Kamil,

If all you need is Java programmatic access to the dictionary service, use dependency injection via Spring.  For example, add the following method to your class:


private ServiceRegistry serviceRegistry = null;

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

Additionally, have Spring initialize your class with an instance of the ServiceRegistry.  Here is an example:

    <bean id="dam.imageProfile" class="org.alfresco.repo.dam.ImageProfile" init-method="init">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
        <property name="authenticationComponent">
            <ref bean="authenticationComponent"/>
        </property>
        <property name="nodeService">
            <ref bean="nodeService"/>
        </property>
        <property name="policyComponent">
            <ref bean="policyComponent"/>
        </property>
    </bean>
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

These property reference refer to beans defined in Spring configuration files elsewhere in the classpath.  If you grep for "ServiceRegistry" in your Alfresco directory, you'll see that many other classes obtain a reference to a particular service via dependency injection, and you can find the one that actually defines the bean.

Anyway, once you have a handle to the ServiceRegistry, you can call getDictionaryService() and use it however you want to.

Thanks,
-Brian