View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.ctv.service;
35  
36  import java.io.FileOutputStream;
37  import java.io.IOException;
38  import java.sql.Connection;
39  import java.text.SimpleDateFormat;
40  import java.util.Date;
41  import java.util.HashMap;
42  import java.util.Map;
43  
44  import org.apache.commons.io.IOUtils;
45  import org.springframework.stereotype.Service;
46  
47  import fr.paris.lutece.plugins.ctv.util.EditionUtils;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPathService;
50  
51  @Service( "editionService" )
52  public class EditionServiceImpl implements IEditionService
53  {
54  
55      private static final String CODE_ERROR_RECAPITULATIF_EMPRISES_TAXATION_SAVOI2 = "LogGenerationDocumentRecapitulatifEmprisesTaxationSavoi2";
56  
57  	@Override
58      public byte [ ] getPermisStationnement( Connection connection, String contextUrl, int numDemande, int empriseId )
59      {
60          String stationnementEditionPath = AppPathService.getPath( "editions.permis.stationnement" );
61          return getEditionData( connection, contextUrl, numDemande, empriseId, stationnementEditionPath );
62      }
63  
64      @Override
65      public byte [ ] getPermissionVoirie( Connection connection, String contextUrl, int numDemande, int empriseId )
66      {
67          String voirieEditionPath = AppPathService.getPath( "editions.permission.voirie" );
68          return getEditionData( connection, contextUrl, numDemande, empriseId, voirieEditionPath );
69      }
70  
71      @Override
72      public byte [ ] getPermissionProlongationOccupation( Connection connection, String contextUrl, int numDemande, int empriseId, int prolongationNum )
73      {
74          String autorisationProlongationOccupationPath = AppPathService.getPath( "editions.Autorisation.prolongation.occupation" );
75          return getAutorisationnData( connection, contextUrl, numDemande, empriseId, autorisationProlongationOccupationPath, prolongationNum );
76      }
77  
78      private byte [ ] getEditionData( Connection connection, String contextUrl, int numDemande, int empriseId, String path )
79      {
80          Map<String, Object> parameters = new HashMap<String, Object>( );
81          parameters.put( "numDemande", numDemande );
82          parameters.put( "empriseId", empriseId );
83          parameters.put( "contextUrl", contextUrl );
84          parameters.put( "REPO_DIR", AppPathService.getPath( "editions.repo.dir" ) );
85          parameters.put( "REPORT_CONNECTION", connection );
86          return EditionUtils.getEdition( connection, path, parameters );
87      }
88  
89      private byte [ ] getAutorisationnData( Connection connection, String contextUrl, int numDemande, int empriseId, String path, int prolongationNum )
90      {
91          Map<String, Object> parameters = new HashMap<String, Object>( );
92          parameters.put( "numDemande", numDemande );
93          parameters.put( "empriseId", empriseId );
94          parameters.put( "contextUrl", contextUrl );
95          parameters.put( "prolongationNum", prolongationNum );
96          parameters.put( "REPO_DIR", AppPathService.getPath( "editions.repo.dir" ) );
97          parameters.put( "REPORT_CONNECTION", connection );
98          return EditionUtils.getEdition( connection, path, parameters );
99      }
100 
101     @Override
102     public byte [ ] getAutorisationProlongationIntervention( Connection connection, String contextUrl, int numDemande, int empriseId, int prolongationNum )
103     {
104         String autorisationProlongationInterventionPath = AppPathService.getPath( "editions.Autorisation.prolongation.intervention" );
105         return getAutorisationnData( connection, contextUrl, numDemande, empriseId, autorisationProlongationInterventionPath, prolongationNum );
106     }
107 
108     @Override
109     public byte [ ] getAutorisationIntervention( Connection connection, String contextUrl, int numDemande, int empriseId )
110     {
111         String autorisationInterventionPath = AppPathService.getPath( "editions.Autorisation.intervention" );
112         return getEditionData( connection, contextUrl, numDemande, empriseId, autorisationInterventionPath );
113     }
114 
115     @Override
116     public byte [ ] getInfoEmprise( Connection connection, String contextUrl, int numDemande, int empriseId )
117     {
118         String infoEmprisePath = AppPathService.getPath( "editions.recap.emprise" );
119         return getEditionData( connection, contextUrl, numDemande, empriseId, infoEmprisePath );
120     }
121     
122     @Override
123     public byte [ ] getInfoEmpriseGarantie( Connection connection, String contextUrl, int numDemande, int empriseId )
124     {
125         String infoEmprisePath = AppPathService.getPath( "editions.recap.emprise.garantie" );
126         byte[] editionData = getEditionData( connection, contextUrl, numDemande, empriseId, infoEmprisePath );
127         if(editionData.length <= 2000) {
128         	logError(CODE_ERROR_RECAPITULATIF_EMPRISES_TAXATION_SAVOI2, "Génération document récapitulatif emprise", numDemande, empriseId);
129         }
130 		return editionData;
131     }
132 
133     @Override
134     public void writeEditionOnServeur( byte [ ] edition, String fileDest, boolean isGarantie, boolean isDU, int demandeId, int empriseId ) throws IOException
135     {
136         FileOutputStream fos = null;
137         try
138         {
139             fos = new FileOutputStream( fileDest );
140             fos.write( edition );
141         }
142         catch (IOException e)
143         {
144         	if(isGarantie) {
145 
146         	if(isDU) {
147         		logError(CODE_ERROR_RECAPITULATIF_EMPRISES_TAXATION_SAVOI2, "Copie document récapitulatif emprise sur "+fileDest, demandeId, empriseId, e);
148         	} else {
149         		logError(CODE_ERROR_RECAPITULATIF_EMPRISES_TAXATION_SAVOI2, "Enregistrement document récapitulatif emprise sur "+fileDest, demandeId, empriseId, e);
150         	}
151         	} else {
152         		AppLogService.error( "Write Edition On Serveur Error : " + e );
153         	}
154         }
155         finally
156         {
157             IOUtils.closeQuietly( fos );
158         }
159     }
160 
161 	private void logError(String codeError, String labelError, int demandeId, int empriseId, Throwable e) {
162 		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
163 		String message = "ERRORCITE "+codeError+"_"+dateFormat.format(new Date())+" : "+labelError+". Demande: "+demandeId+", Emprise: "+empriseId;
164 		if(e != null) {
165 			AppLogService.error(message, e);
166 		} else {
167 			AppLogService.error(message);
168 		}
169 	}
170 	private void logError(String codeError, String labelError, int demandeId, int empriseId) {
171 		logError(codeError, labelError, demandeId, empriseId, null);
172 	}
173     
174     @Override
175     public void writeEditionOnServeur( byte [ ] edition, String fileDest) throws IOException
176     {
177     	writeEditionOnServeur(edition, fileDest, false, false, 0, 0);
178     }
179 
180     @Override
181     public byte [ ] getAnnulationEmprise( Connection connection, String contextUrl, int numDemande, int empriseId )
182     {
183         String annulationEmprisePath = AppPathService.getPath( "editions.annulation.emprise" );
184         return getEditionData( connection, contextUrl, numDemande, empriseId, annulationEmprisePath );
185     }
186 
187 }