Skip to content

8. [TD]: Il livello [business]

Parole chiave: architettura multistrato, Spring, iniezione di dipendenze.

8.1. Assistenza

 

La cartella [support / chap-08] contiene il progetto Eclipse relativo a questo capitolo.

8.2. Configurazione di Maven

  

Il progetto [elections-metier-dao-jdbc] si baserà sul progetto [elections-dao-jdbc-01]. Installeremo il file JAR di quest'ultimo progetto nel repository Maven locale:

 

Una volta fatto ciò, la configurazione Maven per il progetto Eclipse [elections-metier-dao-jdbc] è la seguente:

  

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>istia.st.elections</groupId>
    <artifactId>elections-metier-dao-jdbc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.7.RELEASE</version>
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <!-- DAO -->
        <dependency>
            <groupId>istia.st.elections</groupId>
            <artifactId>elections-dao-jdbc-01</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <!-- plugins -->
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>config.AppConfig</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <!-- to install the project artifact in the local Maven repository -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
            </plugin>
        </plugins>
    </build>
</project>
  • Righe 21–25: la dipendenza dal JAR del progetto [elections-jdbc-01]. Copia queste righe direttamente nel file [pom.xml] del progetto che desideri importare:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>istia.st.elections</groupId>
    <artifactId>elections-dao-jdbc-01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
...
  • Righe 26–37: le dipendenze necessarie per il test. Sebbene siano presenti nel file [pom.xml] del progetto [elections-jdbc-01], dobbiamo includerle qui perché il loro attributo [<scope>test</scope>] indica che non sono state incluse nel file JAR inserito nel repository Maven locale;

8.3. Configurazione Spring

  

package elections.metier.config;
 
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@Import({ elections.dao.config.AppConfig.class })
@EnableCaching
@ComponentScan(basePackages = { "elections.metier.service" })
public class MetierConfig {
}
  • Riga 7: importiamo tutti i bean definiti nel livello [DAO];
  • riga 8: abilitiamo la cache. Non ridefiniamo il bean [CacheManager], poiché è già definito nel livello [DAO];
  • riga 9: il livello [business] definisce nuovi bean nel pacchetto [elections.business.service];

8.4. L'interfaccia [ IElectionsMetier]

  

Il livello [business] disporrà dell'interfaccia presentata nella sezione 4.2. Ecco un riepilogo:


public interface IElectionsMetier {
 
    public ListeElectorale[] getListesElectorales();
 
    public int getNbSiegesAPourvoir();
 
    public double getSeuilElectoral();
 
    public void recordResultats(ListeElectorale[] listesElectorales);
 
    public ListeElectorale[] calculerSieges(ListeElectorale[] listesElectorales);
 
}

8.5. La classe di implementazione [ ElectionsMetier]

  

Questa classe implementa l'interfaccia [IElectionsMetier]. L'implementazione proposta avrà la seguente struttura:


package elections.metier.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
 
import elections.dao.entities.ListeElectorale;
import elections.dao.service.IElectionsDao;
 
@Component
public class ElectionsMetier implements IElectionsMetier {
 
    // the access point to the [dao] layer instantiated by [Spring]
    @SuppressWarnings("unused")
    @Autowired
    private IElectionsDao electionsDao;

    // calculation of seats obtained
  @Override
    public ListeElectorale[] calculerSieges(ListeElectorale[] listesElectorales) {
        throw new RuntimeException("[calculerSieges] not yet implemented");
    }
 
    // competing lists
  @Override
    public ListeElectorale[] getListesElectorales() {
        throw new RuntimeException("[getListesElectorales] not yet implemented");
    }
 
    // saving results
  @Override
    public void recordResultats(ListeElectorale[] listesElectorales) {
        throw new RuntimeException("[recordResultats] not yet implemented");
    }
 
    // number of seats to be filled
    @Override
    public int getNbSiegesAPourvoir() {
        throw new RuntimeException("[getNbSiegesAPourvoir] not yet implemented");
    }
 
    // electoral threshold
    @Override
    public double getSeuilElectoral() {
        throw new RuntimeException("[getSeuilElectoral] not yet implemented");
    }
 
}
  • riga 10: la classe [ElectionsMetier] è un componente Spring;
  • riga 11: la classe [ElectionsMetier] implementa l'interfaccia [IElectionsMetier];
  • righe 15–16: iniezione Spring di un riferimento al livello [DAO];
  • righe 37 e 44: il numero di seggi da assegnare e la soglia elettorale vengono memorizzati nella cache;

Compito: Scrivere la classe [ElectionsMetier]. Utilizzare i commenti dove disponibili. Non tenteremo di intercettare (try/catch) le eccezioni [ElectionsException] che vengono propagate dal livello [DAO]. Lasceremo che si propaghino al livello [UI]. Poiché la classe [ElectionsException] è un tipo di eccezione non controllata, non è necessario gestirla con un blocco try/catch.


8.6. La classe di test

  

La classe di test JUnit avrà la seguente struttura:


package tests;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import dao.config.AppConfig;
import dao.entities.ElectionsException;
import metier.service.IElectionsMetier;
 
@SpringApplicationConfiguration(classes = MetierConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Test01 {
 
    // business] layer
    @Autowired
    static private IElectionsMetier electionsMetier;
 
    /**
     * vérification 1 : méthode de calcul des sièges on fixe en dur les listes
     */
    @Test
    public void calculSieges1() {
        // create a table of the 7 candidate lists (name, vote)
 
        // the seats for each list are calculated
 
        // we check the results (seats, votes, eliminates)
 
        // temporary
        throw new RuntimeException("[calculSieges1] not yet implemented");
    }
 
    /**
     * vérification 2 : méthode de calcul des sièges on demande les listes à la
     * couche [metier] puis on fixe en dur les voix
     */
    @Test
    public void calculSieges2() {
        // the table of 7 candidate lists is retrieved as a base
 
        // the voices are hard-fixed
 
        // the seats obtained by each list are calculated
 
        // we check the results (seats, votes, eliminates)
 
        // temporary
        throw new RuntimeException("[calculSieges2] not yet implemented");
    }
 
    /**
     * vérification 3 méthode de calcul des sièges on provoque une exception
     */
    @Test(expected = ElectionsException.class)
    // write a test that causes an exception of type [ElectionsException]
    public void calculSieges3() {
        // create a table of 25 candidate lists, each with 1 vote
        // all 25 lists will have the same number of votes (4%)
 
        // calculation of seats - normally there should be a ElectionsException
        // with an electoral threshold of 5%
 
        // temporary
        throw new RuntimeException("[calculSieges3] not yet implemented");
    }
 
    /**
     * enregistrement des résultats de l'élection
     */
    @Test
    public void ecritureResultatsElections() {
        // create the table of 7 candidate lists
 
        // the voices are hard-fixed
 
        // the seats obtained by each list are calculated
 
        // results are entered into the database
 
        // reread lists in base
 
        // we check the results (seats, votes, eliminates)
 
        // temporary
        throw new RuntimeException("[ecritureResultatsElections] not yet implemented");
    }
}

Compito: Scrivere i quattro metodi di test utilizzando i commenti come guida. Ricordare che quando questi metodi vengono eseguiti, il campo [electionsMetier] alla riga 19 è già stato inizializzato. Verificare che il test JUnit abbia esito positivo.


8.7. Creazione dell'archivio del livello [business]

Come abbiamo fatto per il livello [DAO], inseriamo l'archivio del progetto [elections-metier-dao-jdbc] nel repository Maven locale:

 

Nota: questa operazione potrebbe non andare a buon fine se il progetto Eclipse è associato a un JRE (Java Runtime Environment) anziché a un JDK (Java Development Kit). Per verificarlo, segui le istruzioni riportate nella sezione 3.1. Se ti accorgi di avere un JRE e non un JDK, associa il progetto a un JDK come descritto in quella sezione.

8.8. Conclusione

Rivediamo l'architettura generale dell'applicazione [Elections] che stiamo realizzando:

Abbiamo realizzato i livelli [business] e [DAO]. Ora realizzeremo il livello [UI]. Proporremo due implementazioni per questo livello:

  • un'implementazione "da console"
  • un'implementazione con interfaccia utente grafica