AnsweredAssumed Answered

Error When creating new aspect with a dropdown list from DB

Question asked by croc on Aug 5, 2010
Hi,

I am creating an aspect which has a property that select values from the DB(database).

I am getting the error below:
Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925
The problematic instruction:
———-
==> if node.hasAspect("cm:versionable") && node.versionHistory?size != 0 [on line 9, column 7 in org/alfresco/slingshot/documentlibrary/item.lib.ftl]
in user-directive itemLib.itemJSON [on line 37, column 10 in org/alfresco/slingshot/documentlibrary/doclist.get.json.ftl]
———-

Java backtrace for programmers:
———-
freemarker.template.TemplateModelException: Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925
   at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
   at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.Expression.isTrue(Expression.java:138)
   at freemarker.core.AndExpression.isTrue(AndExpression.java:68)
   at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:77)
   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.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:208)
   at freemarker.core.Macro$Context.runMacro(Macro.java:164)
   at freemarker.core.Environment.visit(Environment.java:601)
   at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
   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.EscapeBlock.accept(EscapeBlock.java:84)
   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:197)
   at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:578)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:263)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:143)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:367)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:325)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:417)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:434)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:298)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:319)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:177)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:116)
   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:127)
   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:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:619)
Caused by: org.alfresco.service.namespace.NamespaceException: A namespace prefix is not registered for uri http://www.alfresco.com/model/knowledgebase/1.0
   at org.alfresco.service.namespace.QName.toPrefixString(QName.java:403)
   at org.alfresco.repo.template.BaseContentNode.hasAspect(BaseContentNode.java:321)
   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:597)
   at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:835)
   at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
   … 53 more
18:00:48,770  ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 07050001 Wrapped Exception (with status template): 07050001 Error during processing of the template 'Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925'. Please contact your system administrator.
org.springframework.extensions.webscripts.WebScriptException: 07050001 Wrapped Exception (with status template): 07050001 Error during processing of the template 'Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925'. Please contact your system administrator.
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:740)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:167)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:367)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:325)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:417)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:434)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:298)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:319)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:177)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:116)
   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:127)
   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:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:619)
Caused by: org.alfresco.service.cmr.repository.TemplateException: 07050001 Error during processing of the template 'Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925'. Please contact your system administrator.
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:201)
   at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:578)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:263)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:143)
   … 21 more
Caused by: freemarker.template.TemplateModelException: Method public boolean org.alfresco.repo.template.BaseContentNode.hasAspect(java.lang.String) threw an exception when invoked on Node Type: {http://www.alfresco.org/model/content/1.0}content   Node Ref: workspace://SpacesStore/4ca9d813-bcd7-4d4e-b7db-e48a84784925
   at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130)
   at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
   at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
   at freemarker.core.Expression.isTrue(Expression.java:138)
   at freemarker.core.AndExpression.isTrue(AndExpression.java:68)
   at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:77)
   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.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:208)
   at freemarker.core.Macro$Context.runMacro(Macro.java:164)
   at freemarker.core.Environment.visit(Environment.java:601)
   at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
   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.EscapeBlock.accept(EscapeBlock.java:84)
   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:197)
   … 24 more
Caused by: org.alfresco.service.namespace.NamespaceException: A namespace prefix is not registered for uri http://www.alfresco.com/model/knowledgebase/1.0
   at org.alfresco.service.namespace.QName.toPrefixString(QName.java:403)
   at org.alfresco.repo.template.BaseContentNode.hasAspect(BaseContentNode.java:321)
   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:597)
   at freemarker.ext.beans.BeansWrapper.invokeMethod(BeansWrapper.java:835)
   at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:106)
   … 53 more

below is my XMl:
<?xml version="1.0" encoding="UTF-8"?>

<!– Definition of new Model –>

<!– The important part here is the name - Note: the use of the my: namespace
     which is defined further on in the document –>
<model name="kb:knowledgebase" xmlns="http://www.alfresco.org/model/dictionary/1.0">

   <!– Optional meta-data about the model –>
   <description>Knowledge Base Model</description>
   <author>Will Abson</author>
   <version>1.0</version>

   <!– Imports are required to allow references to definitions in other models –>
   <imports>
        <!– Import Alfresco Dictionary Definitions –>
      <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
      <!– Import Alfresco Content Domain Model Definitions –>
      <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
   </imports>


   <!– Introduction of new namespaces defined by this model –>
   <!– NOTE: The following namespace my.new.model should be changed to reflect your own namespace –>
   <namespaces>
            <namespace uri="http://www.alfresco.com/model/knowledgebase/1.0" prefix="kb"/>

   </namespaces>

  <!– Note: intentionally empty list of values - allowed values can be added/updated dynamically –>

<constraints>
      <constraint name="kb:allowedValues" type="org.alfresco.module.org_alfresco_module_dod5015.action.impl.ListOfValuesQueryConstraint">
         <title>Colors</title>
         <parameter name="allowedValues">
            <list>
            </list>
         </parameter>
         <parameter name="caseSensitive"><value>true</value></parameter>
      </constraint>
</constraints>

<!– Definition of new Content Aspect: Knowledge Base Document –>
    <aspects>
      <aspect name="kb:referencable">
         <title>Knowledge Base Referencable</title>
         <properties>
            <property name="kb:documentRef">
               <type>d:text</type>
            </property>
        
    <property name="kb:sFormatVersion">
                <title>Colors</title>
                <type>d:text</type>
                <mandatory>true</mandatory>
                <index enabled="true">
          <atomic>true</atomic>
          <stored>false</stored>
          <tokenised>false</tokenised>
          </index>
                <constraints>
                    <constraint ref="kb:allowedValues" />
                </constraints>
            </property>
   </properties>
      </aspect>
   </aspects>

</model>

Below is the javaclass:
package org.alfresco.module.org_alfresco_module_dod5015.action.impl;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.alfresco.repo.dictionary.constraint.AbstractConstraint;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.dictionary.DictionaryException;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;

/**
* Constraint implementation that ensures the value is one of a constrained
* <i>list of values</i>.  By default, this constraint is case-sensitive.
*
* @see #setAllowedValues(List)
* @see #setCaseSensitive(boolean)
*
*/
public class ListOfValuesQueryConstraint extends AbstractConstraint
{
    private static final String ERR_NO_VALUES = "d_dictionary.constraint.list_of_values.no_values";
    private static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string";
    private static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value";

    private List<String> allowedValues;
    private List<String> allowedValuesUpper;
    private boolean caseSensitive;
   
    public ListOfValuesQueryConstraint()
    {
        caseSensitive = true;
    }
   
    /**
     * {@inheritDoc}
     */
    @Override
    public String getType()
    {
        return "LIST";
    }
   
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder(80);
        sb.append("ListOfValuesConstraint")
          .append("[ allowedValues=").append(allowedValues)
          .append(", caseSensitive=").append(caseSensitive)
          .append("]");
        return sb.toString();
    }

    /**
     * Get the allowed values.  Note that these are <tt>String</tt> instances, but may
     * represent non-<tt>String</tt> values.  It is up to the caller to distinguish.
     *
     * @return Returns the values allowed
     */
    public List<String> getAllowedValues()
    {
        return allowedValues;
    }
   
    /**
     * Set the values that are allowed by the constraint.
     * 
     * @param values a list of allowed values
     */
   
    public void setAllowedValues(List<String> allowedValues)
    {
        if (allowedValues == null)
        {
            throw new DictionaryException(ERR_NO_VALUES);
        }
        int valueCount = allowedValues.size();
        if (valueCount == 0)
        {
            throw new DictionaryException(ERR_NO_VALUES);
        }
        this.allowedValues = Collections.unmodifiableList(allowedValues);
        // make the upper case versions
        this.allowedValuesUpper = new ArrayList<String>(valueCount);
        for (String allowedValue : this.allowedValues)
        {
            allowedValuesUpper.add(allowedValue.toUpperCase());
        }
    }

    /**
     * @return Returns <tt>true</tt> if this constraint is case-sensitive (default)
     */
    public boolean isCaseSensitive()
    {
        return caseSensitive;
    }

    /**
     * Set the handling of case checking.
     *
     * @param caseSensitive <tt>true</tt> if the constraint is case-sensitive (default),
     *      or <tt>false</tt> for case-insensitive.
     */
    public void setCaseSensitive(boolean caseSensitive)
    {
        this.caseSensitive = caseSensitive;
    }

    @Override
    public void initialize()
    {
        super.initialize();
        checkPropertyNotNull("allowedValues", allowedValues);
        //super.setCaseSensitive(false);
      this.loadDB();
    }
   
    @Override
    public Map<String, Object> getParameters()
    {
        Map<String, Object> params = new HashMap<String, Object>(2);
       
        params.put("caseSensitive", this.caseSensitive);
        params.put("allowedValues", this.allowedValues);
       
        return params;
    }

    protected void evaluateSingleValue(Object value)
    {
        // convert the value to a String
        String valueStr = null;
        try
        {
            valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
        }
        catch (TypeConversionException e)
        {
            throw new ConstraintException(ERR_NON_STRING, value);
        }
        // check that the value is in the set of allowed values
        if (caseSensitive)
        {
            if (!allowedValues.contains(valueStr))
            {
                throw new ConstraintException(ERR_INVALID_VALUE, value);
            }
        }
        else
        {
            if (!allowedValuesUpper.contains(valueStr.toUpperCase()))
            {
                throw new ConstraintException(ERR_INVALID_VALUE, value);
            }
        }
    }
   
    protected void loadDB() {

      String driverName = "com.mysql.jdbc.Driver";
      String serverName = "localhost";
      String mydatabase = "alfresco";
      String username = "root";
      String password = "root";

      List<String> av = new ArrayList<String>();
      List<String> al = new ArrayList<String>();

      try {
         Connection connection = null;
         Class.forName(driverName);
         String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
         connection = DriverManager.getConnection(url, username, password);
         Statement stmt = connection.createStatement();
         ResultSet rs = stmt.executeQuery("select value,name from DDLIST");
         while (rs.next()) {
            av.add(rs.getString("value"));
            al.add(rs.getString("name"));
         }
      } catch (Exception e) {
      }

      //super.setAllowedValues(av);
      this.setAllowedValues(al);
   }
   
}
I have edited this class from another one that i got from the code.

Below is my test class, and it is working:
package org.alfresco.module.org_alfresco_module_dod5015.action.impl;

import java.util.List;


public class ListOfValuesQueryConstraintTest {

   /**
    * @param args
    */

   public static void main(String[] args) { // TODO Auto-generated method stub

      ListOfValuesQueryConstraint loc = new ListOfValuesQueryConstraint();
      loc.loadDB();
      List<String> vName = loc.getAllowedValues();
      for (String string : vName) {
         System.out.println("Color name: "+string.toString()+"Values: "+loc.getTitle());         
      }
      
      
      
   }

}

Please help me guys, i need this very urgent.

Thanks,
Croc

Outcomes