Partage
  • Partager sur Facebook
  • Partager sur Twitter

Activité Compléter le projet de gestion de tickets

    10 septembre 2019 à 12:04:41

    Bonjour à tous.

    Je suis en train de travailler sur l'activité du cours Simplifiez le développement d'applications Java avec Spring et je dois avouez que j'ai beaucoup de mal.

    Je suis à la première étape concernant la création du fichier texte via le module batch. Lorsque je lance le main je me prends l'erreur [1;31mERROR[m [main] org.example.demo.ticket.batch.Main : java.lang.IllegalArgumentException: Property 'dataSource' is required.

    Ma datesource est pourtant bien configuré dans le batchContext.xml comme dans le cours:

    	<bean id="dataSourceTicketConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    		<property name="location" value="file:${TICKET_HOME}/conf/db-ticket.properties" />
    	</bean>
    
    	<bean id="dataSourceTicket"
    		  class="org.apache.commons.dbcp2.BasicDataSourceFactory"
    		  factory-method="createDataSource"
    		  destroy-method="close">
    		<constructor-arg ref="dataSourceTicketConfiguration" />	  
    	</bean>


     Et je l'injecte dans l'abstractDaoImpl:

    package org.example.demo.ticket.consumer.impl.dao;
    
    import javax.inject.Inject;
    import javax.inject.Named;
    import javax.sql.DataSource;
    
    public class AbstractDaoImpl {
    	@Inject
    	@Named("dataSourceTicket")
    	private DataSource dataSource;
    	
    	protected DataSource getDataSource() {
    		return dataSource;
    	}
    
    	public void setDatasource(DataSource datasource) {
            this.dataSource = datasource;
        }
    }
    

    Du coup je ne comprends pas pourquoi il m'indique qu'elle n'est pas présente. POurriez vous m'aider s'il vous plait?

    Le main du module batch:

    package org.example.demo.ticket.batch;
    
    
    import java.io.PrintWriter;
    import java.util.List;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.example.demo.ticket.business.contract.manager.ManagerFactory;
    import org.example.demo.ticket.model.bean.ticket.TicketStatut;
    import org.example.demo.ticket.model.exception.TechnicalException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    
    /**
     * Classe Principale de lancement des Batches.
     *
     * @author lgu
     */
    @Component
    public class Main {
    
        /** Logger pour la classe */
        private static final Log LOGGER = LogFactory.getLog(Main.class);
    
    
        /**
         * The entry point of application.
         *
         * @param pArgs the input arguments
         * @throws TechnicalException sur erreur technique
         */
        @SuppressWarnings("resource")
    	public static void main(String[] pArgs) throws TechnicalException {
        	ApplicationContext vApplicationContext = new ClassPathXmlApplicationContext("classpath:/bootstrapContext.xml");
        	// Properties exportTicketPath = (Properties) vApplicationContext.getBean("exportTicketPath");
        	//String path = exportTicketPath.getProperty("path");
        	String path ="${config.exportpath}";
            ManagerFactory vManagerFactory = vApplicationContext.getBean("managerFactory", ManagerFactory.class);
        	
        	try {
                if (pArgs.length < 1) {
                    throw new TechnicalException("Veuillez préciser le traitement à effectuer !");
                }
    
                String vTraitementId = pArgs[0];
                if ("ExportTicketStatus".equals(vTraitementId)) {
                    LOGGER.info("Execution du traitement : ExportTicketStatus");
                    // ...
                    List<TicketStatut> vList = vManagerFactory.getTicketManager().getListTicketStatus();
                    PrintWriter writer = new PrintWriter(path.concat("/ticket-statut.txt"));
                    for (TicketStatut ticketStatut: vList) {
                    	writer.println(ticketStatut.toString());
                    }
                    writer.flush();
                } else {
                    throw new TechnicalException("Traitement inconnu : " + vTraitementId);
                }
            } catch (Throwable vThrowable) {
                LOGGER.error(vThrowable);
                System.exit(1);
            }
        }
    }
    

    Le TicketManagerImpl:

    package org.example.demo.ticket.business.impl.manager;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.inject.Inject;
    import javax.inject.Named;
    
    import org.example.demo.ticket.business.contract.manager.TicketManager;
    import org.example.demo.ticket.model.bean.projet.Projet;
    import org.example.demo.ticket.model.bean.ticket.Bug;
    import org.example.demo.ticket.model.bean.ticket.Commentaire;
    import org.example.demo.ticket.model.bean.ticket.Evolution;
    import org.example.demo.ticket.model.bean.ticket.HistoriqueStatut;
    import org.example.demo.ticket.model.bean.ticket.Ticket;
    import org.example.demo.ticket.model.bean.ticket.TicketStatut;
    import org.example.demo.ticket.model.bean.utilisateur.Utilisateur;
    import org.example.demo.ticket.model.exception.NotFoundException;
    import org.example.demo.ticket.model.exception.TechnicalException;
    import org.example.demo.ticket.model.recherche.ticket.RechercheTicket;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.TransactionStatus;
    import org.springframework.transaction.support.DefaultTransactionDefinition;
    import org.springframework.transaction.support.TransactionCallback;
    import org.springframework.transaction.support.TransactionCallbackWithoutResult;
    import org.springframework.transaction.support.TransactionTemplate;
    
    
    public class TicketManagerImpl extends AbstractManager implements TicketManager {	
    	/**
         * Cherche et renvoie le {@link Ticket} numéro {@code pNumero}
         *
         * @param pNumero le numéro du Ticket
         * @return Le {@link Ticket}
         * @throws NotFoundException Si le Ticket n'est pas trouvé
         */
        @Override
    	public Ticket getTicket(Long pNumero) throws NotFoundException {
            // Je n'ai pas encore codé la DAO
            // Je mets juste un code temporaire pour commencer le cours...
            if (pNumero < 1L) {
                throw new NotFoundException("Ticket non trouvé : numero=" + pNumero);
            }
            Evolution vEvolution = new Evolution(pNumero);
            vEvolution.setPriorite(10);
            return vEvolution;
        }
    
    
        /**
         * Renvoie la liste des {@link Ticket} correspondants aux critères de recherche.
         *
         * @param pRechercheTicket -
         * @return List
         */
        @Override
    	public List<Ticket> getListTicket(RechercheTicket pRechercheTicket) {
            // Je n'ai pas encore codé la DAO
            // Je mets juste un code temporaire pour commencer le cours...
            List<Ticket> vList = new ArrayList<>();
            if (pRechercheTicket.getProjetId() != null) {
                Projet vProjet = new Projet(pRechercheTicket.getProjetId());
                for (int vI = 0; vI < 4; vI++) {
                    Ticket vTicket = new Bug((long) pRechercheTicket.getProjetId() * 10 + vI);
                    vTicket.setProjet(vProjet);
                    vList.add(vTicket);
                }
            } else {
                for (int vI = 0; vI < 9; vI++) {
                    Ticket vTicket = new Evolution((long) vI);
                    vList.add(vTicket);
                }
            }
            return vList;
        }
    
    
        /**
         * Renvoie le nombre de {@link Ticket} correspondants aux critères de recherche.
         *
         * @param pRechercheTicket -
         * @return int
         */
        @Override
    	public int getCountTicket(RechercheTicket pRechercheTicket) {
            // Je n'ai pas encore codé la DAO
            // Je mets juste un code temporaire pour commencer le cours...
            return 42;
        }
        
        @Override
        public void changerStatut(Ticket pTicket, TicketStatut pNewStatut, Utilisateur pUtilisateur, Commentaire pCommentaire) {
        	TransactionStatus vTransactionStatus = getPlatformTransactionManager().getTransaction(new DefaultTransactionDefinition());
        	try {
        		pTicket.setStatut(pNewStatut);
        		getDaoFactory().getTicketDao().updateTicket(pTicket);
        		
        		TransactionStatus vTScommit = vTransactionStatus;
        		vTransactionStatus = null;
        		getPlatformTransactionManager().commit(vTScommit);
        	} finally {
        		if (vTransactionStatus != null) {
        			getPlatformTransactionManager().rollback(vTransactionStatus);
        		}
        	}
        }
        
        @Override
        public List<TicketStatut> getListTicketStatus() {
        	List<TicketStatut> vList = getDaoFactory().getTicketDao().getListStatut();
        	return vList;
        }
    }
    

    Et le TicketDaoImpl:

    package org.example.demo.ticket.consumer.impl.dao;
    
    import java.sql.Types;
    import java.util.List;
    
    import org.example.demo.ticket.consumer.contract.dao.TicketDao;
    import org.example.demo.ticket.consumer.impl.rowmapper.TicketStatutRowMapper;
    import org.example.demo.ticket.model.bean.ticket.Ticket;
    import org.example.demo.ticket.model.bean.ticket.TicketStatut;
    import org.example.demo.ticket.model.recherche.ticket.RechercheTicket;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
    import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    
    public class TicketDaoImpl extends AbstractDaoImpl implements TicketDao {
    	@Override
    	public int getCountTicket(RechercheTicket pRechercheTicket) {
    		MapSqlParameterSource vParams = new MapSqlParameterSource();
    		StringBuilder vSql = new StringBuilder("SELECT COUNT(*) FROM ticket WHERE 1=1");
    		
    		if (pRechercheTicket != null) {
    			if (pRechercheTicket.getAuteurId() != null) {
    				vSql.append(" AND auteur_id = :auteur_id");
    				vParams.addValue("auteur_id", pRechercheTicket.getAuteurId());
    			}
    			
    			if (pRechercheTicket.getProjetId() != null) {
    				vSql.append(" AND projet_id = :projet_id");
    				vParams.addValue("projet_id", pRechercheTicket.getProjetId());
    			}
    		}
    		
    		NamedParameterJdbcTemplate vJdbcTemplate = new NamedParameterJdbcTemplate(getDataSource());
    		int vNbreTicket = vJdbcTemplate.queryForObject(vSql.toString(), vParams, Integer.class);
    		
    		return vNbreTicket;
    	}
    	
    	@Override
    	public List<TicketStatut> getListStatut() {
    		String vSql = "SELECT * FROM public.statut";
    		JdbcTemplate vJdbcTemplate = new JdbcTemplate(getDataSource());
    		
    		RowMapper<TicketStatut> vRowMapper = new TicketStatutRowMapper();
    		
    		List<TicketStatut> vListStatut = vJdbcTemplate.query(vSql, vRowMapper);
    		
    		return vListStatut;
    	}
    	
    	@Override
    	public void updateTicketStatut(TicketStatut pTicketStatut) {
    		String vSql = "UPDATE statut SET libelle = :libelle WHERE id = :id";
    		
    		BeanPropertySqlParameterSource vParams = new BeanPropertySqlParameterSource(pTicketStatut);
    		vParams.registerSqlType("libelle", Types.VARCHAR);
    		vParams.registerSqlType("id", Types.INTEGER);
    		
    		NamedParameterJdbcTemplate vJdbcTemplate = new NamedParameterJdbcTemplate(getDataSource());
    		
    		int vNbreLignMaj = vJdbcTemplate.update(vSql, vParams);
    	}
    
    	@Override
    	public void updateTicket(Ticket pTicket) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    
    • Partager sur Facebook
    • Partager sur Twitter
      12 septembre 2019 à 9:54:44

      Petit up.
      Si quelqu'un n'a ne serait ce qu'un début de piste, je suis preneur :).
      Merci beaucoup.
      • Partager sur Facebook
      • Partager sur Twitter

      Activité Compléter le projet de gestion de tickets

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown