com.bakeryfactory.pcp.servidor.PcpOpCabecalhoGridAction.java Source code

Java tutorial

Introduction

Here is the source code for com.bakeryfactory.pcp.servidor.PcpOpCabecalhoGridAction.java

Source

/*
 * The MIT License
 *
 * Copyright 2016 Claudinei Aparecido Perboni - contact:cperbony@gmail.com.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.bakeryfactory.pcp.servidor;

import com.bakeryfactory.padrao.java.Constantes;
import com.bakeryfactory.padrao.servidor.HibernateUtil;
import com.bakeryfactory.pcp.java.PcpOpCabecalhoVO;
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 07/10/2016
 */
public class PcpOpCabecalhoGridAction implements Action {

    public PcpOpCabecalhoGridAction() {
    }

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

    @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 PCP_OP_CABECALHO from com.bakeryfactory.pcp.java.PcpOpCabecalhoVO as PCP_OP_CABECALHO";
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Response res = HibernateUtils.getBlockFromQuery(pars.getAction(), pars.getStartPos(),
                    Constantes.TAMANHO_BLOCO, pars.getFilteredColumns(), pars.getCurrentSortedColumns(),
                    pars.getCurrentSortedVersusColumns(), com.bakeryfactory.pcp.java.PcpOpCabecalhoVO.class,
                    baseSQL, new Object[0], new Type[0], "PCP_OP_CABECALHO", HibernateUtil.getSessionFactory(),
                    session);
            return res;
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ErrorResponse(ex.getMessage());
        } finally {
            try {
                session.close();
            } catch (Exception ex1) {
            }
        }

    }

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

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

    private 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");

            PcpOpCabecalhoVO vo = null;

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

            for (int i = 0; i < persistentObjects.size(); i++) {
                vo = (PcpOpCabecalhoVO) 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) {
            }
        }
    }
}