Connect External Databse

cancel
Showing results for 
Search instead for 
Did you mean: 
Senior Member II

Connect External Databse

I am trying to connect external database from alfresco to get constraint like below way.

<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="${jdbcDriver}" />
      <property name="url" value="${jdbcUrl}" />
      <property name="username" value="${jdbcUserName}" />
      <property name="password" value="${jdbcPassword}" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <constructor-arg ref="DataSource" />
</bean>

<bean id="documentDAOImpl" class="com.dao.DocumentDAOImpl">
   <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

<bean id="batchTypeConstraints" class="com.constraints.BatchTypeConstraints">
      <property name="documentDAOImpl" ref="documentDAOImpl" />
</bean>

public class BatchTypeConstraints extends ListOfValuesConstraint implements Serializable {

   private DocumentDAOImpl documentDAOImpl;

   public DocumentDAOImpl getDocumentDAOImpl() {
   return documentDAOImpl;
}

public void setDocumentDAOImpl(DocumentDAOImpl documentDAOImpl) {
      this.documentDAOImpl = documentDAOImpl;
}

@Override
public void setAllowedValues(List allowedValues) {
}

@Override
public void setCaseSensitive(boolean caseSensitive) {
}

public void initialize() {
super.setCaseSensitive(false);
this.getDataFromDb();
}

protected void getDataFromDb() {
try {
      List<MetaDataModel> pShortNames = documentDAOImpl.getAllBatchByWorkFlowType();
      List<String> allowedValue = new ArrayList<String>();
for (MetaDataModel pShortName : pShortNames) {
   allowedValue.add(pShortName.getName());
}
super.setAllowedValues(allowedValue);
} catch (Exception e) {
e.printStackTrace();
}

}
}

It's giving me null pointer error at this line :

List<MetaDataModel> pShortNames = documentDAOImpl.getAllBatchByWorkFlowType();

I think data base is not connecting.

How Can i connect ?

Thanks & Regard,
Sanjay
11 Replies
Highlighted
Master

Re: Connect External Databse

How did you register that constraint with the model? Did you use the class name as the type of the constraint in the data model? Then that is the cause for the problem - when you use the class name as the type, the class is instantiated using the default constructor without any parameters passed into it. So instead of using your defined bean, it uses an unitialized instance where of course the field for your DAO is null. You need to use a registered constraint to reference your Spring bean.

Highlighted
Senior Member II

Re: Connect External Databse

Like this way,

<constraint name="mbs:batchTypeAspectList" type="com.mbs.constraints.BatchTypeConstraints">
<parameter name="allowedValues">
<list>

</list>
</parameter>
</constraint>

Thanks & Regard,
Sanjay
Highlighted
Senior Member II

Re: Connect External Databse

How to use registered constraint ? Can you Give me some idea?

Thanks & Regard,
Sanjay
Highlighted
Master

Re: Connect External Databse

See the Alfresco code for smart folders:

Highlighted
Senior Member II

Re: Connect External Databse

Hi,

I have defined constraint as per above link but it is giving me error.

Bean : 


<bean id="batchType" class="com.mbs.constraints.BatchTypeConstraints" init-method="initialize">
<property name="shortName">
<value>batchType</value>
</property>
<property name="documentDAOImpl" ref="documentDAOImpl" />
</bean>

Constraint :

<constraint name="mbs:batchTypeAspectList"
type="REGISTERED">
<parameter name="registeredName">
<value>batchType</value>
</parameter>
</constraint>

Error : 

There is no constraint registered by name 'batchType'.

Thanks & Regard,
Sanjay
Highlighted
Senior Member II

Re: Connect External Databse

I am using SDK2 for customization.

Thanks & Regard,
Sanjay
Highlighted
Master

Re: Connect External Databse

Please read again what I have linked. The constraint been needs to be registered with the constraint registry, which are set as properties on the bean. The bean class (which I did not link but would have been easy to find) extends from AbstractConstraint which contains all the logic and properties for registration.

Highlighted
Senior Member II

Re: Connect External Databse

Still Error.

<bean id="batchType" class="com.mbs.constraints.BatchTypeConstraints" init-method="initialize">
<property name="shortName">
<value>batchType</value>
</property>
<property name="registry">
<ref bean="cm:constraintRegistry" />
</property>
<property name="sorted" value="true" />
<property name="documentDAOImpl" ref="documentDAOImpl" />
</bean>

Thanks & Regard,
Sanjay
Highlighted
Senior Member II

Re: Connect External Databse

Can you guide me how can i implement?

Thanks & Regard,
Sanjay