cancel
Showing results for 
Search instead for 
Did you mean: 

Connect External Databse

sanjaybandhaniya
Elite Collaborator
Elite Collaborator

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 ?

11 REPLIES 11

Make sure your Spring file is actually loaded / processed. When building a module it should be in alfresco/module/<moduleId>/module-context.xml file (or a file imported there). If you are just doing customisation without really building a proper module (you SHOULD consider building a module), then it can be any alfresco/extension/<xyz>-context.xml file

sanjaybandhaniya
Elite Collaborator
Elite Collaborator

Thanks Axel Faust‌ for help.

I have solved this way.

Constraints:

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

Bean:

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

Class:


public class BatchTypeConstraints extends ListOfValuesConstraint implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2561245100862732935L;
@Autowired
DocumentDAOImpl documentDaoImpl;

public BatchTypeConstraints() {
super();
}

@Override
public List<String> getRawAllowedValues() {
List<String> result = new ArrayList<>();
List<MetaDataModel> providers = documentDaoImpl.getAllBatchByWorkFlowType();
for (MetaDataModel data : providers) {
result.add(data.getName());
}
super.setAllowedValues(result);
return result;
}

@Override
public List<String> getAllowedValues() {
return super.getAllowedValues();
}

@Override
protected void evaluateSingleValue(Object value) {
super.setAllowedValues(getAllowedValues());
}

}