Package org.magicbox.ibatis

Source Code of org.magicbox.ibatis.UtentiDaoImpl

/*
* Copyright Massimiliano Dess� (desmax74@yahoo.it)
*
* Licensed under Apache License Version 2.0
* (http://www.apache.org/licenses/LICENSE-2.0),
*
* for commercial use, under
* GNU General Public License Version 2 or later (the "GPL")
* http://www.gnu.org/licenses/gpl.html
*/
package org.magicbox.ibatis;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javolution.util.FastList;
import javolution.util.FastMap;

import org.apache.log4j.Logger;
import org.magicbox.dao.UtentiDao;
import org.magicbox.domain.UtenteLight;
import org.magicbox.domain.UtenteUltraLightImpl;
import org.magicbox.domain.UtenteUltraLight;
import org.magicbox.domain.Utente;
import org.magicbox.dto.Email;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapExecutor;

/**
* Interfaccia di accesso al repository degli utenti
*
* @author Massimiliano Dess� (desmax74@yahoo.it)
* @since jdk 1.6.0
* @version 3.0
*/
@SuppressWarnings("unchecked")
public class UtentiDaoImpl extends SqlMapClientDaoSupport implements UtentiDao {

    public int aggiornaUtenti(final List<String> ids, final long idGruppo) {
        return (Integer) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) {
                int ris = 0;
                try {

                    executor.startBatch();
                    Iterator<String> iter = ids.iterator();

                    while (iter.hasNext()) {
                        UtenteUltraLight utente = new UtenteUltraLightImpl("", idGruppo, Long.parseLong(iter.next().toString()));
                        executor.insert("updateGruppoUtente", utente);
                    }
                    ris = executor.executeBatch();
                } catch (SQLException e) {
                    Logger log = Logger.getLogger(this.getClass());
                    StringBuffer sb = new StringBuffer("aggiornaUtenti failed \n").append("num ids batch:").append(ids.size()).append("\n").append(" idgruppo:").append(idGruppo).append("\n").append(
                            e.getNextException());
                    log.error(sb.toString());
                    ;
                }
                return ris;
            }
        });
    }

    public boolean updateCancellazioneGruppo(long idGruppo) {
        return getSqlMapClientTemplate().update("updateCancellazioneGruppo", idGruppo) > 0 ? true : false;
    }

    public List<UtenteLight> getUtentiGruppo(long idCentro, long idGruppo) {
        Map params = new FastMap();
        params.put("idCentro", idCentro);
        params.put("idGruppo", idGruppo);
        return getSqlMapClientTemplate().queryForList("getUtentiGruppo", params);
    }

    public List<UtenteLight> getUtentiCentroLiberi(long idCentro) {
        return getSqlMapClientTemplate().queryForList("getUtentiLiberi", idCentro);
    }

    public List<Email> getListaMailCentro(long idCentro) {
        return getSqlMapClientTemplate().queryForList("getEmailUtentiCentro", idCentro);
    }

    public List<String> getListaCellulariCentro(long idCentro) {
        return getSqlMapClientTemplate().queryForList("getNumeroCellulareUtentiCentro", idCentro);
    }

    public List<Utente> getUtentiCentroReport(long idCentro) {
        return getSqlMapClientTemplate().queryForList("getUtentiCentroReport", idCentro);
    }

    public List<Utente> getUtentiByAlfabetico(String lettera, long idCentro, int pagina) {

        // attenzione alle sql injection, deve arrivare una lettera di un solo
        // carattere
        if (lettera.length() > 1) {
            return new FastList(0);
        } else {
            Map params = new FastMap();
            params.put("letteraLower", (new StringBuilder(lettera.toLowerCase().concat("%")).toString()));
            params.put("letteraUpper", (new StringBuilder(lettera.toUpperCase().concat("%")).toString()));
            params.put("centro", idCentro);
            params.put("limit", offset);
            params.put("offset", pagina * offset);
            return getSqlMapClientTemplate().queryForList("getUtentiByAlfabetico", params);
        }
    }

    public int deleteAllUtenti(long idCentro) {
        return getSqlMapClientTemplate().delete("deleteAllUtenti", idCentro);
    }

    public int getNumeroUtentiPerPagina() {
        return offset;
    }

    public int getNumeroUtentiCentro(long idCentro) {
        return (Integer) getSqlMapClientTemplate().queryForObject("selectCountNumeroUtentiCentro", idCentro);
    }

    public boolean deleteUtente(long id, long idCentro) throws DataAccessException {
        Map params = new FastMap();
        params.put("id", id);
        params.put("centro", idCentro);
        return getSqlMapClientTemplate().delete("deleteUtente", params) == 1 ? true : false;
    }

    public Utente getUtente(long id, long idCentro) throws DataAccessException {
        Map params = new FastMap();
        params.put("id", id);
        params.put("centro", idCentro);
        return (Utente) getSqlMapClientTemplate().queryForObject("getUtente", params);
    }

    public List<UtenteLight> getUtentiByCap(String cap, long idCentro, int pagina) throws DataAccessException {
        Map params = new FastMap();
        params.put("cap", cap);
        params.put("idCentro", idCentro);
        params.put("limit", offset);
        params.put("offset", pagina * offset);
        return getSqlMapClientTemplate().queryForList("getUtentiByCap", params);
    }

    public List<Utente> getUtentiByCitta(String citta, long idCentro, int pagina) throws DataAccessException {
        Map params = new FastMap();
        params.put("citta", citta);
        params.put("idCentro", idCentro);
        params.put("limit", offset);
        params.put("offset", pagina * offset);
        return getSqlMapClientTemplate().queryForList("getUtentiByCitta", params);
    }

    public List<Utente> getUtentiByNominativo(String nominativo, long idCentro, int pagina) throws DataAccessException {
        Map params = new FastMap();
        params.put("nominativoLower", (new StringBuilder(nominativo.toLowerCase().concat("%")).toString()));
        // params.put("nominativoUpper", (new
        // StringBuilder(nominativo.toUpperCase().concat("%")).toString()));
        params.put("idCentro", idCentro);
        params.put("limit", offset);
        params.put("offset", pagina * offset);
        return getSqlMapClientTemplate().queryForList("getUtentiByNominativo", params);
    }

    public List<Utente> getUtentiByProvincia(String provincia, long idCentro, int pagina) throws DataAccessException {
        Map params = new FastMap();
        params.put("provincia", provincia);
        params.put("idCentro", idCentro);
        params.put("limit", offset);
        params.put("offset", pagina * offset);
        return getSqlMapClientTemplate().queryForList("getUtentiByProvincia", params);
    }

    public List<UtenteLight> getUtentiCentro(long idCentro, int pagina) throws DataAccessException {
        Map params = new FastMap();
        params.put("idCentro", idCentro);
        params.put("limit", offset);
        params.put("offset", pagina * offset);
        return getSqlMapClientTemplate().queryForList("getUtentiCentro", params);
    }

    public Long insertUtente(Utente utente) throws DataAccessException {
        return (Long) getSqlMapClientTemplate().insert("insertUtente", utente);
    }

    public int updateUtente(Utente utente) throws DataAccessException {
        return getSqlMapClientTemplate().update("updateUtente", utente);
    }

    public int insertUtenti(final List<Utente> utenti, final long idCentro) throws DataAccessException {
        return (Integer) getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) {
                int ris = 0;
                try {

                    executor.startBatch();
                    Iterator<Utente> iter = utenti.iterator();

                    while (iter.hasNext()) {
                        Utente utente = (Utente) iter.next();
                        executor.insert("insertUtente", utente);
                    }
                    ris = executor.executeBatch();
                } catch (SQLException e) {
                    Logger log = Logger.getLogger(this.getClass());
                    StringBuffer sb = new StringBuffer("InsertUtenti failed \n").append("num user batch:").append(utenti.size()).append("\n").append(" center:").append(idCentro).append("\n").append(
                            e.getNextException());
                    log.error(sb.toString());
                }
                // richiedo la deallocazione degli utenti inseriti che potrebbero essere parecchi
                System.gc();
                return ris;
            }
        });

    }

    public void setNumeroUtentiPerPagina(int offset) {
        this.offset = offset;
    }

    private int offset;
}
TOP

Related Classes of org.magicbox.ibatis.UtentiDaoImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.