Joget Workflow v6 Beta 4 Released

Check out the latest Joget Workflow v6 Beta for many new features and improvements in user experience (UX), app maintainability and performance.

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 3

...

Code Block
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



    <bean id="productSessionFactory" class="org.springframework.orm.hibernate4hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="setupDataSource"/>
        <property name="mappingResources">
            <list>
                <value>/org/joget/sample/products/model/Products.hbm.xml</value>
             </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>



    <bean id="productsDao" class="org.joget.products.dao.ProductsDaoImpl">
        <property name="sessionFactory" ref="productSessionFactory" />
        <property name="localSessionFactory" ref="&amp;productSessionFactory"/>
    </bean>



</beans>

In the application context, I created 2 beans. Bean "productSessionFactory" is to initialize a session factory with the hibernate mapping file. Bean "productsDao" is to initialize the dao object of my sample plugin.

...

Code Block
package org.joget.products;

import org.joget.apps.app.service.AppUtil;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppContext {

    private static AppContext instance;
    private AbstractApplicationContext appContext;

    public synchronized static AppContext getInstance() {
        if (instance == null) {
            instance = new AppContext();
        }
        return instance;
    }

    private AppContext() {
        Thread currentThread = Thread.currentThread();
        ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
        try {
            currentThread.setContextClassLoader(this.getClass().getClassLoader());
            this.appContext = new ClassPathXmlApplicationContext(new String[]{"/productsApplicationContext.xml"}, this.getClass(), AppUtil.getApplicationContext());
        } finally {
            currentThread.setContextClassLoader(threadContextClassLoader);
        }
    }

    public AbstractApplicationContext getAppContext() {
        return appContext;
    }
}

 

You need to orverride findSession method in  ProductsDaoImpl as compared to joget version 4.

Code Block
languagejava
package org.joget.products.dao;
import java.util.Collection;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.joget.commons.spring.model.AbstractSpringDao;
import org.joget.commons.util.LogUtil;
import org.joget.products.model.Product;

public class ProductsDaoImpl extends AbstractSpringDao implements ProductsDao {

    @Override
    public Session findSession() {
        Session session = null;
        SessionFactory sf = super.getSessionFactory();
        
        try {
            session = sf.getCurrentSession();
        } catch (Exception e) {}
        
        if (session == null) {
            session = sf.openSession();
        }
        
        return session;
    }
    
    public Boolean addProduct(Product product) {
        try {
            save("Products", product);
            return true;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Add Product Error!");
            return false;
        }
    }

    public Boolean updateProduct(Product product) {
        try {
            merge("Products", product);
            return true;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Update Product Error!");
            return false;
        }
    }

    public Boolean deleteProduct(String id) {
        try {
            Product product = getProduct(id);
            if (product != null) {
          
                delete("Products", product);
            }
            return true;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Delete Product Error!");
            return false;
        }
    }

    public Product getProduct(String id) {
        try {
            return (Product) find("Products", id);
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Get Product Error!");
            return null;
        }
    }

    public Collection<Product> getProducts() {
        try {
            Collection products = super.find("Products", "", null, null, null, null, null);
            return products;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Get Products Error!");
        }
        return null;
    }
  
}

 

After you implemented your POJO and dao class, you should be able to use your dao in your plugin as following. Please refer to the attached sample plugin for POJO and dao implementation.

...