Переглянути джерело

modifica query per griglie frontend

Fabio Dalle Ave 2 місяців тому
батько
коміт
eb87034e36

+ 9 - 1
pom.xml

@@ -99,10 +99,18 @@
             <version>1.9.4</version>
         </dependency>
 
+<!--
         <dependency>
             <groupId>com.lowagie</groupId>
             <artifactId>itext</artifactId>
-            <version>2.0.7</version>
+            <version>2.0.8</version>
+        </dependency>
+-->
+
+        <dependency>
+            <groupId>com.github.librepdf</groupId>
+            <artifactId>openpdf</artifactId>
+            <version>2.0.3</version>
         </dependency>
 
         <dependency>

+ 2 - 0
src/main/java/commons/utility/dao/GenericDAO.java

@@ -4,6 +4,7 @@ import java.io.BufferedReader;
 import java.io.Reader;
 import java.io.Serializable;
 
+import jakarta.transaction.Transactional;
 import org.hibernate.Hibernate;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
@@ -16,6 +17,7 @@ import java.sql.Clob;
 import java.util.ArrayList;
 import java.util.List;
 
+@Transactional
 public class GenericDAO<T, ID extends Serializable>
         implements IDAO<T, ID> {
 

+ 2 - 1
src/main/java/it/powerage/Application.java

@@ -2,11 +2,12 @@ package it.powerage;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
 import org.springframework.boot.builder.SpringApplicationBuilder;
 import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
 import org.springframework.context.annotation.ComponentScan;
 
-@SpringBootApplication
+@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
 @ComponentScan(basePackages = {
     "commons",
     "powerage",

+ 1 - 3
src/main/java/powerage/controller/LoginController.java

@@ -53,10 +53,8 @@ public class LoginController extends BaseController {
 		setRequest(request);
 		setResponse(response);
 
-		utenteDao.startTransaction();
 		Utente utente = utenteDao.login(loginBean.getUsername(), loginBean.getPassword());
-		utenteDao.commit();
-		
+
 		if (utente == null)
 			return "redirect:login.do";
 		else

+ 1 - 1
src/main/java/powerage/controller/PrintController.java

@@ -13,7 +13,7 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 
-import com.lowagie.text.pdf.codec.Base64.OutputStream;
+// import com.lowagie.text.pdf.codec.Base64.OutputStream;
 
 import powerage.dao.OrdineDAO;
 import powerage.entities.Ordine;

+ 0 - 1
src/main/java/powerage/dao/AgenteDAO.java

@@ -11,7 +11,6 @@ import org.hibernate.query.Query;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 
-
 import powerage.entities.Agente;
 import commons.utility.dao.GenericDAO;
 import commons.utility.dao.IDAO;

+ 1 - 1
src/main/java/powerage/dao/LogDAO.java

@@ -26,7 +26,7 @@ public class LogDAO extends GenericDAO<Log, Long> implements IDAO<Log, Long> {
 		}
 
 		Query<Long> countQuery = currentSession()
-			.createQuery("select count(l.idLog) " + hql.toString(), Long.class);
+			.createQuery("select count(l) " + hql.toString(), Long.class);
 		if (filter != null && !filter.isEmpty()) {
 			countQuery.setParameter("filter", "%" + filter + "%");
 			countQuery.setParameter("filterExact", filter);

+ 20 - 12
src/main/java/powerage/dao/OrdineDAO.java

@@ -1,11 +1,12 @@
 package powerage.dao;
 
-import java.util.ArrayList;
 import java.util.List;
 
 import commons.utility.MutableHolder;
 
-import org.hibernate.SessionFactory;
+import jakarta.persistence.EntityManager;
+import jakarta.persistence.TypedQuery;
+import lombok.RequiredArgsConstructor;
 import org.hibernate.query.Query;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,7 +17,7 @@ import powerage.entities.Ordine;
 import commons.utility.dao.IDAO;
 
 @Repository
-public class OrdineDAO 
+public class OrdineDAO
 		extends PowerAgeDAO<Ordine, Long> 
 		implements IDAO<Ordine, Long>  {
 
@@ -30,7 +31,7 @@ public class OrdineDAO
 			hql.append(" and (c.ragioneSociale like :filter or c.codCliente = :filterExact or a.ragioneSociale = :filterRS or a.codAgente = :filterCA)");
 		}
 
-		Query<Long> countQuery = currentSession()
+		TypedQuery<Long> countQuery = currentSession()
 			.createQuery("select count(o.idOrdine) " + hql.toString(), Long.class);
 		if (idAgente != 0)
 			countQuery.setParameter("idAgente", idAgente);
@@ -40,7 +41,8 @@ public class OrdineDAO
 			countQuery.setParameter("filterRS", filter);
 			countQuery.setParameter("filterCA", filter);
 		}
-		rowCount.value = countQuery.uniqueResult().intValue();
+
+		rowCount.value = (countQuery.getSingleResult()).intValue();
 
 		Query<Ordine> query = currentSession()
 			.createQuery(hql.toString() + " order by o.dataOrdine desc", Ordine.class);
@@ -59,6 +61,7 @@ public class OrdineDAO
 		return res;
 	}
 
+
 	public List<Ordine> getOrdiniNuovi(int idAgente, String filter, int iDisplayStart, int iDisplayLength, int iSortCol_0, String sSortDir_0, MutableHolder<Integer> rowCount) {
 		boolean tx = startTransaction();
 
@@ -102,15 +105,19 @@ public class OrdineDAO
 
 	public List<Ordine> getNewOrders() {
 		boolean tx = startTransaction();
-		List<Ordine> res = currentSession()
-			.createQuery("from Ordine o where o.stato = :stato", Ordine.class)
+
+		Query<Ordine> q = currentSession()
+			.createQuery("select o from Ordine o where o.stato = :stato", Ordine.class)
 			.setParameter("stato", "N")
-			.list();
+			;
+
+		List<Ordine> res = q.list();
 		if (tx)
 			commit();
 		return res;
 	}
 
+
 	public Ordine getByIdOrdineAgente(long idAgente, long idOrdineAgente) {
 		List<Ordine> res = currentSession()
 			.createQuery("from Ordine o join fetch o.agente a where a.idAgente = :idAgente and o.idOrdineAgente = :idOrdAgente", Ordine.class)
@@ -128,7 +135,7 @@ public class OrdineDAO
 
 		boolean tx = startTransaction();
 
-		StringBuilder hql = new StringBuilder("from Ordine o join  o.cliente c join  o.agente a where 1=1 ");
+		StringBuilder hql = new StringBuilder("from Ordine o join o.cliente c join o.agente a where 1=1 ");
 		if (beanFiltriOrdine != null) {
 			if (beanFiltriOrdine.getCbSoloNuovi())
 				hql.append(" and o.stato = :stato");
@@ -145,8 +152,9 @@ public class OrdineDAO
 				hql.append(" and o.dataOrdine <= :aData");
 		}
 
-		Query<Long> countQuery = currentSession()
-			.createQuery("select count(distinct o) " + hql.toString(), Long.class);
+
+		jakarta.persistence.Query countQuery =  currentSession().createQuery("select count(distinct o)" + hql.toString());
+
 		if (beanFiltriOrdine != null) {
 			if (beanFiltriOrdine.getCbSoloNuovi())
 				countQuery.setParameter("stato", K_NUOVO);
@@ -163,7 +171,7 @@ public class OrdineDAO
 			if (beanFiltriOrdine.getaData() != null)
 				countQuery.setParameter("aData", beanFiltriOrdine.getaData());
 		}
-		Object cnt = countQuery.uniqueResult();
+		Object cnt = ((Number)(countQuery.getSingleResult())).longValue();
 		if (cnt instanceof Long)
 			rowCount.value = ((Long) cnt).intValue();
 		else

+ 3 - 2
src/main/java/powerage/dao/UtenteDAO.java

@@ -2,8 +2,6 @@ package powerage.dao;
 
 import java.util.List;
 
-import org.hibernate.SessionFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
 
 import powerage.entities.Utente;
@@ -31,10 +29,13 @@ public class UtenteDAO
 	}
 
 	public Utente login(String login, String password) {
+		boolean tx = startTransaction();
 		List<Utente> utenti = currentSession()
 			.createQuery("from Utente u where u.login = :login", Utente.class)
 			.setParameter("login", login)
 			.list();
+		if (tx)
+			commit();
 		if (utenti.size()>0){
 			String pwdUtente = utenti.get(0).getPassword();
 			if (pwdUtente==null)

+ 1 - 1
src/main/java/powerage/entities/ListinoArticolo.java

@@ -14,7 +14,7 @@ import commons.common.CalendarJSonSerializer;
 @Table(name="Listini_articolo")
 public class ListinoArticolo {
 	@Id
-	@GeneratedValue
+	@GeneratedValue( strategy = GenerationType.IDENTITY)
 	@Column(name="idListinoArticoli")
 	private Long idListinoArticoli;
 	

+ 6 - 1
src/main/java/powerage/exporters/impl/OrdiniExporter.java

@@ -106,7 +106,12 @@ public class OrdiniExporter extends BaseExporter<Ordine> {
 		linea += StringUtility.fillString(" ", 5, ' ');  //abi
 		linea += StringUtility.fillString(" ", 12, ' ');  //conto
 		
-		linea += StringUtility.fillString(ordine.getNoteOrdine().substring(0, Math.min(ordine.getNoteOrdine().length(), 180)), 180, ' ');
+		linea += StringUtility.fillString(
+			(ordine.getNoteOrdine() != null
+				? ordine.getNoteOrdine().substring(0, Math.min(ordine.getNoteOrdine().length(), 180))
+				: " "
+			), 180, ' ');
+
 		linea += StringUtility.fillString(" ", 3, ' ');  //porto
 		linea += StringUtility.fillString(ordine.getCliente().getUbicazione(), 3, ' ');  //spedizioniere
 		linea += StringUtility.fillString("", 3, ' ');  //tipologia ordine

+ 12 - 3
src/main/java/powerage/importers/impl/ArticoliImporter.java

@@ -38,7 +38,7 @@ public class ArticoliImporter extends BaseImporter implements IBaseImporter {
 		Articolo articolo;
 		Tabella classifica1 = null, classifica2 = null, classifica3 = null, classifica4 = null;
 		String codArticolo, descrizione, unitaMisura, categoria, codGruppo, codInventario, codIVA, codStatistico;
-		float prezzoBase, pezziConfezione, pesoUnitario, numColliMinimo;
+		Float prezzoBase, pezziConfezione, pesoUnitario, numColliMinimo;
 		boolean changed;
 		
 		codArticolo = getNextField(15);
@@ -69,8 +69,17 @@ public class ArticoliImporter extends BaseImporter implements IBaseImporter {
 		setCurrentIndex(292);
 		prezzoBase = Float.parseFloat(getNextField(17)) / 1000000;  // 6 decimali
 
-		// fixme
-		numColliMinimo = 0;
+		// il numero di colli minumo è a colonna 2553 lungo 6
+		setCurrentIndex(2552);
+		String sNumColliMinimo = getNextField(6);
+		numColliMinimo = null;
+		// controlla che sNumColliMinimo esista e sia numerico
+		if (sNumColliMinimo != null && sNumColliMinimo.matches("\\d+")) {
+			numColliMinimo = Float.parseFloat(sNumColliMinimo);
+		} else {
+			sNumColliMinimo = null;
+		}
+
 
 		// Cerca l'articolo
 		articolo = articoloDAO.getByCode(codArticolo);

+ 15 - 6
src/main/java/powerage/importers/impl/BaseImporter.java

@@ -23,6 +23,7 @@ public abstract class BaseImporter implements IBaseImporter {
 	private int currentIndex = 0;
 	protected Calendar dataModifica;
 	protected boolean changed;
+	boolean tx = false;
 	private PowerAgeDAO dao;
 	private boolean useTransactions = true;
 	private int lineNumber;
@@ -144,16 +145,24 @@ public abstract class BaseImporter implements IBaseImporter {
 	public abstract void processLine();
 	
 	public void startTransaction() {
-		if (useTransactions)
-			sessionFactory.getCurrentSession().beginTransaction();
+		if (useTransactions) {
+			tx = dao.startTransaction();
+		}
+
 	}
 	public void commit() {
-		if (useTransactions)
-			sessionFactory.getCurrentSession().getTransaction().commit();
+		if (useTransactions && tx) {
+			dao.commit();
+//			sessionFactory.getCurrentSession().getTransaction().commit();
+		}
 	}
 	public void rollback() {
-		if (useTransactions)
-			sessionFactory.getCurrentSession().getTransaction().rollback();
+		if (useTransactions && tx)  {
+			dao.rollback();
+			tx = false;
+			// sessionFactory.getCurrentSession().getTransaction().rollback();
+		}
+
 	}
 	
 	

+ 25 - 7
src/main/java/powerage/importers/impl/ClientiImporter.java

@@ -2,6 +2,8 @@
 
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.hibernate.HibernateException;
 import org.springframework.stereotype.Repository;
@@ -48,6 +50,10 @@ public class ClientiImporter extends BaseImporter implements IBaseImporter {
 	private int attivo;
 	Cliente cliente;
 
+	Map<String, Agente> agenti =  new HashMap<>();
+	Map<String, Listino> listini =  new HashMap<>();
+	Map<String, Pagamento> pagamenti =  new HashMap<>();
+
 //	public ClientiImporter() {
 //		super(false);
 //	}
@@ -80,15 +86,27 @@ public class ClientiImporter extends BaseImporter implements IBaseImporter {
 //		if (cliente==null && partitaIVA!=null && !partitaIVA.isEmpty())
 //			cliente = clienteDAO.getByPartitaIVA(partitaIVA);
 //		if (cliente==null && codiceFiscale!=null && !codiceFiscale.isEmpty())
-//			cliente = clienteDAO.getByCodiceFiscale(codiceFiscale);   \
-		
-		
-		agente = agenteDAO.getByCode(codAgente);
+//			cliente = clienteDAO.getByCodiceFiscale(codiceFiscale);
+
+		if (!agenti.containsKey(codAgente)) {
+			agente = agenteDAO.getByCode(codAgente);
+			agenti.put(codAgente, agente);
+		}
+		agente = agenti.get(codAgente);
+
 		if (agente == null) {
 			
-		} else { 
-			listino = listinoDAO.getByCode(codListino);
-			pagamento = pagamentoDAO.getByCode(codPagamento);			
+		} else {
+			if (!listini.containsKey(codListino)) {
+				listini.put(codListino, listinoDAO.getByCode(codListino));
+			}
+			listino = listini.get(codListino);
+
+			if (!pagamenti.containsKey(codPagamento)) {
+				pagamenti.put(codPagamento, pagamentoDAO.getByCode(codPagamento));
+			}
+
+			pagamento = pagamenti.get(codPagamento);
 			
 				if (cliente == null) {
 					cliente = new Cliente();

+ 28 - 4
src/main/java/powerage/importers/impl/ListinoArticoloImporter.java

@@ -2,6 +2,8 @@ package powerage.importers.impl;
 
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Repository;
@@ -33,6 +35,11 @@ public class ListinoArticoloImporter extends BaseImporter implements IBaseImport
 	private Articolo articolo;
 	private Listino listino;
 	private Sconto sconto;
+
+	// cache
+	Map<String, Articolo> articoli = new HashMap<>();
+	Map<String, Listino> listini = new HashMap<>();
+	Map<String, Sconto> sconti = new HashMap<>();
 	
 	public ListinoArticoloImporter() {
 		super();
@@ -74,11 +81,28 @@ public class ListinoArticoloImporter extends BaseImporter implements IBaseImport
 		sPrezzo = getNextField(10) + getNextField(6);
 		prezzo = Double.parseDouble(sPrezzo) / 1000000;
 		codSconto = getNextField(3);
-		
-		listino = listinoDAO.getByCode(codListino);
-		articolo = articoloDAO.getByCode(codArticolo);
+
+		if (listini.get(codListino)==null) {
+			listini.put(codListino, listinoDAO.getByCode(codListino));
+		}
+
+		listino = listini.get(codListino); // listinoDAO.getByCode(codListino);
+
+		if (articoli.get(codArticolo)==null) {
+			articoli.put(codArticolo, articoloDAO.getByCode(codArticolo));
+		}
+		articolo = articoli.get(codArticolo);
+
+		//articolo = articoloDAO.getByCode(codArticolo);
+		if (sconti.get(codSconto)==null) {
+			sconti.put(codSconto, scontoDAO.getByCode(codSconto));
+		}
+		sconto = sconti.get(codSconto);
+		// sconto = scontoDAO.getByCode(codSconto);\
+
+
 		listinoArticolo = lisArtDAO.getListinoArticolo(codListino, codArticolo, dataInizio);
-		sconto = scontoDAO.getByCode(codSconto);
+
 		
 		// Controlla che il listino e l'articolo esistano
 		if (listino==null) {

+ 1 - 1
src/main/java/powerage/stampe/StampaOrdine.java

@@ -16,7 +16,7 @@ import com.lowagie.text.pdf.PdfImportedPage;
 import com.lowagie.text.pdf.PdfReader;
 import com.lowagie.text.pdf.PdfStamper;
 import com.lowagie.text.pdf.PdfWriter;
-import com.lowagie.text.pdf.codec.Base64.OutputStream;
+// import com.lowagie.text.pdf.codec.Base64.OutputStream;
 
 import powerage.entities.Ordine;
 import powerage.entities.RigaOrdine;