com.bakeryfactory.calendarioproducao.servidor.ProducaoSalaGridAction.java Source code

Java tutorial

Introduction

Here is the source code for com.bakeryfactory.calendarioproducao.servidor.ProducaoSalaGridAction.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bakeryfactory.calendarioproducao.servidor;

import com.bakeryfactory.calendarioproducao.java.ProducaoSalaVO;
import com.bakeryfactory.padrao.java.Constantes;
import com.bakeryfactory.padrao.servidor.HibernateUtil;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;
import org.hibernate.type.Type;
import org.openswing.swing.message.receive.java.ErrorResponse;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.VOListResponse;
import org.openswing.swing.message.send.java.GridParams;
import org.openswing.swing.server.Action;
import org.openswing.swing.server.UserSessionParameters;
import org.openswing.swing.util.server.HibernateUtils;

/**
 * @author Claudinei Aparecido Perboni - contact:cperbony@gmail.com
 * @date   15/01/2017
 */
public class ProducaoSalaGridAction implements Action {

    public ProducaoSalaGridAction() {
    }

    @Override
    public String getRequestName() {
        return "reuniaoSalaGridAction";
    }

    @Override
    public Response executeCommand(Object inputPar, UserSessionParameters userSessionPars,
            HttpServletRequest request, HttpServletResponse response, HttpSession userSession,
            ServletContext context) {
        GridParams pars = (GridParams) inputPar;
        Integer acao = (Integer) pars.getOtherGridParams().get("acao");

        switch (acao) {
        case Constantes.LOAD: {
            return load(inputPar, userSessionPars, request, response, userSession, context);
        }
        case Constantes.INSERT: {
            return insert(inputPar, userSessionPars, request, response, userSession, context);
        }
        case Constantes.UPDATE: {
            return update(inputPar, userSessionPars, request, response, userSession, context);
        }
        case Constantes.DELETE: {
            return delete(inputPar, userSessionPars, request, response, userSession, context);
        }
        }
        return null;
    }

    private Response load(Object inputPar, UserSessionParameters userSessionPars, HttpServletRequest request,
            HttpServletResponse response, HttpSession userSession, ServletContext context) {
        Session session = null;
        GridParams pars = (GridParams) inputPar;
        String baseSQL = "select PRODUCAO_SALA from com.bakeryfactory.calendarioproducao.java.ProducaoSalaVO as PRODUCAO_SALA";
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Response res = HibernateUtils.getBlockFromQuery(pars.getAction(), pars.getStartPos(),
                    Constantes.TAMANHO_BLOCO, // block size...
                    pars.getFilteredColumns(), pars.getCurrentSortedColumns(), pars.getCurrentSortedVersusColumns(),
                    com.bakeryfactory.calendarioproducao.java.ProducaoSalaVO.class, baseSQL, new Object[0],
                    new Type[0], "PRODUCAO_SALA", HibernateUtil.getSessionFactory(), session);
            return res;
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ErrorResponse(ex.getMessage());
        } finally {
            try {
                session.close();
            } catch (Exception ex1) {
            }
        }
    }

    public Response insert(Object inputPar, UserSessionParameters userSessionPars, HttpServletRequest request,
            HttpServletResponse response, HttpSession userSession, ServletContext context) {
        return null;
    }

    public Response update(Object inputPar, UserSessionParameters userSessionPars, HttpServletRequest request,
            HttpServletResponse response, HttpSession userSession, ServletContext context) {
        return null;
    }

    public Response delete(Object inputPar, UserSessionParameters userSessionPars, HttpServletRequest request,
            HttpServletResponse response, HttpSession userSession, ServletContext context) {
        Session session = null;
        try {
            GridParams pars = (GridParams) inputPar;
            ArrayList persistentObjects = (ArrayList) pars.getOtherGridParams().get("persistentObjects");

            ProducaoSalaVO vo = null;

            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            for (int i = 0; i < persistentObjects.size(); i++) {
                vo = (ProducaoSalaVO) persistentObjects.get(i);
                session.delete(vo);
                session.flush();
            }

            session.getTransaction().commit();
            return new VOListResponse(persistentObjects, false, persistentObjects.size());
        } catch (Exception ex) {
            if (session != null) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
            return new ErrorResponse(ex.getMessage());
        } finally {
            try {
                if (session != null) {
                    session.close();
                }
            } catch (Exception ex1) {
            }
        }
    }

}