com.goplace.service.generic.implementation.MetaServiceGenImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.goplace.service.generic.implementation.MetaServiceGenImpl.java

Source

/*
 * Copyright (C) July 2014 Rafael Aznar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.goplace.service.generic.implementation;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Constructor;
import java.sql.Connection;
import java.util.ArrayList;
import com.goplace.dao.generic.implementation.TableDaoGenImpl;
import com.goplace.helper.ExceptionBooster;
import com.goplace.service.publicinterface.MetaServiceInterface;

public abstract class MetaServiceGenImpl implements MetaServiceInterface {

    protected Connection oConnection = null;
    protected String strObjectName = null;
    protected String strPojo = null;

    public MetaServiceGenImpl(String ob, String pojo, Connection con) {
        strObjectName = Character.toUpperCase(ob.charAt(0)) + ob.substring(1);
        oConnection = con;
        strPojo = Character.toUpperCase(pojo.charAt(0)) + pojo.substring(1);
    }

    @Override
    public void setSource(String source) throws Exception {
        strObjectName = source;
    }

    @Override
    public void setPojo(String pojo) throws Exception {
        strPojo = Character.toUpperCase(pojo.charAt(0)) + pojo.substring(1);
    }

    @Override
    public String getPrettyColumns() throws Exception {
        String data = null;
        try {
            oConnection.setAutoCommit(false);
            Constructor c = Class
                    .forName("com.goplace.dao.generic.specific.implementation." + strPojo + "DaoGenSpImpl")
                    .getConstructor(String.class, Connection.class);
            TableDaoGenImpl oDao = (TableDaoGenImpl) c.newInstance(strObjectName, oConnection);
            ArrayList<String> alColumns = null;
            alColumns = oDao.getPrettyColumnsNames();
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();
            data = gson.toJson(alColumns);
        } catch (Exception ex) {
            oConnection.rollback();
            ExceptionBooster.boost(
                    new Exception(this.getClass().getName() + ":getPrettyColumns ERROR: " + ex.getMessage()));
        } finally {
            oConnection.commit();
        }
        return data;
    }

    @Override
    public String getColumns() throws Exception {
        String data = null;
        try {
            oConnection.setAutoCommit(false);
            Constructor c = Class
                    .forName("com.goplace.dao.generic.specific.implementation." + strPojo + "DaoGenSpImpl")
                    .getConstructor(String.class, Connection.class);
            TableDaoGenImpl oDao = (TableDaoGenImpl) c.newInstance(strObjectName, oConnection);
            ArrayList<String> alColumns = null;
            alColumns = oDao.getColumnsNames();
            GsonBuilder gsonBuilder = new GsonBuilder();
            Gson gson = gsonBuilder.create();
            data = gson.toJson(alColumns);
        } catch (Exception ex) {
            oConnection.rollback();
            ExceptionBooster
                    .boost(new Exception(this.getClass().getName() + ":getColumns ERROR: " + ex.getMessage()));
        } finally {
            oConnection.commit();
        }
        return data;
    }
}