View Javadoc
1   package fr.paris.lutece.plugins.atelieraba.web;
2   
3   import java.io.BufferedInputStream;
4   import java.io.FileNotFoundException;
5   import java.io.IOException;
6   import java.io.InputStreamReader;
7   import java.util.Map;
8   
9   import javax.servlet.http.HttpServletRequest;
10  
11  import org.apache.commons.fileupload.FileItem;
12  import org.apache.commons.lang.StringUtils;
13  import org.apache.tika.parser.txt.CharsetDetector;
14  import org.apache.tika.parser.txt.CharsetMatch;
15  
16  import au.com.bytecode.opencsv.CSVReader;
17  import fr.paris.lutece.plugins.atelieraba.business.Cours;
18  import fr.paris.lutece.plugins.atelieraba.business.CoursHome;
19  import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
20  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
21  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
22  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
23  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
24  
25  /**
26   * This class provides the user interface to manage Cours features ( manage )
27   */
28  @Controller( controllerJsp = "ManageCours.jsp", controllerPath = "jsp/admin/plugins/atelieraba/", right = "ATELIERABA_COURS_MANAGEMENT" )
29  public class CoursJspBean extends MVCAdminJspBean
30  {
31  
32      /**
33       *
34       */
35      private static final long   serialVersionUID                 = 956238933185317587L;
36  
37      // Views
38      private static final String VIEW_MANAGE_COURS                = "manageCours";
39      // Templates
40      private static final String TEMPLATE_MANAGE_COURS            = "/admin/plugins/atelieraba/manage_cours.html";
41  
42      // Properties for page titles
43      private static final String PROPERTY_PAGE_TITLE_MANAGE_COURS = "atelieraba.manage_cours.pageTitle";
44      private static final String ERROR_IMPORT_BAD_FILE            = "atelieraba.import.error.lengthFile";
45      private static final String ERROR_IMPORT_BAD_LENGTH_HEADER   = "atelieraba.import.error.lengthHeader";
46      private static final String ERROR_IMPORT_BAD_EXTENSION_FILE  = "atelieraba.import.error.extensionFile";
47      private static final String ERROR_IMPORT_BAD_FILENOTFOUND    = "atelieraba.import.error.filenotfound";
48      private static final String ERROR_IMPORT_BAD_UNKNOWN         = "atelieraba.import.error.unknown";
49      private static final int    EXPECTED_LINE_LENGTH             = 7;
50  
51      // Actions
52      private static final String ACTION_IMPORT_COURS              = "import_cours";
53  
54      /**
55       * Build the Manage View
56       *
57       * @param request
58       *            The HTTP request
59       * @return The page
60       */
61      @View( value = VIEW_MANAGE_COURS, defaultView = true )
62      public String getManageCours( HttpServletRequest request )
63      {
64  
65          Map<String, Object> model = getModel( );
66  
67          return getPage( PROPERTY_PAGE_TITLE_MANAGE_COURS, TEMPLATE_MANAGE_COURS, model );
68      }
69  
70      /**
71       * Process the import/export
72       *
73       * @param request
74       *            The Http Request
75       * @return The Jsp URL of the process result
76       */
77      /**
78       * @param request
79       * @return
80       */
81      @Action( ACTION_IMPORT_COURS )
82      public String doImport( HttpServletRequest request )
83      {
84          MultipartHttpServletRequest multipartRequest = ( MultipartHttpServletRequest ) request;
85          FileItem fileSource = multipartRequest.getFile( "file" );
86          CSVReader reader;
87  
88          try
89          {
90              BufferedInputStream bis = new BufferedInputStream( fileSource.getInputStream( ) );
91              CharsetDetector cd = new CharsetDetector( );
92              cd.setText( bis );
93              CharsetMatch cm = cd.detect( );
94              String charset = cm.getName( );
95              String filename = fileSource.getName( );
96  
97              if ( !filename.endsWith( ".csv" ) )
98              {
99                  addError( ERROR_IMPORT_BAD_EXTENSION_FILE, getLocale( ) );
100 
101                 return redirectView( request, VIEW_MANAGE_COURS );
102             }
103 
104             reader = new CSVReader( new InputStreamReader( fileSource.getInputStream( ), charset ), ';' );
105             String[] header = reader.readNext( );
106 
107             if ( header.length != EXPECTED_LINE_LENGTH )
108             {
109                 addError( ERROR_IMPORT_BAD_LENGTH_HEADER, getLocale( ) );
110                 addError( EXPECTED_LINE_LENGTH + "" );
111 
112                 return redirectView( request, VIEW_MANAGE_COURS );
113             }
114 
115             String[] nextLine;
116             int lineNumber = 1;
117 
118             while ( ( nextLine = reader.readNext( ) ) != null )
119             {
120 
121                 lineNumber++;
122 
123                 if ( nextLine.length != EXPECTED_LINE_LENGTH )
124                 {
125                     addError( ERROR_IMPORT_BAD_FILE, getLocale( ) );
126                     addError( lineNumber + "" );
127 
128                     return redirectView( request, VIEW_MANAGE_COURS );
129                 }
130 
131                 if ( StringUtils.isNotBlank( nextLine[0] ) && StringUtils.isNotBlank( nextLine[1] ) && StringUtils.isNotBlank( nextLine[4] ) )
132                 {
133                     Cours coursExistant = CoursHome.findByCode( nextLine[0], nextLine[1], nextLine[4] );
134 
135                     if ( coursExistant != null )
136                     {
137                         coursExistant.setDescriptionAtelier( nextLine[2] );
138                         coursExistant.setSite( nextLine[3] );
139                         coursExistant.setPlacesDisponibles( Integer.parseInt( nextLine[5] ) );
140                         coursExistant.setPlacesListeAttente( Integer.parseInt( nextLine[6] ) );
141                         CoursHome.update( coursExistant );
142                     } else
143                     {
144                         Cours cours = new Cours( );
145                         cours.setDiscipline( nextLine[0] );
146                         cours.setTitreAtelier( nextLine[1] );
147                         cours.setDescriptionAtelier( nextLine[2] );
148                         cours.setSite( nextLine[3] );
149                         cours.setCreneau( nextLine[4] );
150                         cours.setPlacesDisponibles( Integer.parseInt( nextLine[5] ) );
151                         cours.setPlacesListeAttente( Integer.parseInt( nextLine[6] ) );
152                         CoursHome.create( cours );
153                     }
154                 }
155             }
156             reader.close( );
157         } catch ( FileNotFoundException e )
158         {
159             addError( ERROR_IMPORT_BAD_FILENOTFOUND, getLocale( ) );
160             addError( e.toString( ) );
161 
162             return redirectView( request, VIEW_MANAGE_COURS );
163         } catch ( IOException e )
164         {
165             addError( ERROR_IMPORT_BAD_UNKNOWN, getLocale( ) );
166             addError( e.toString( ) );
167 
168             return redirectView( request, VIEW_MANAGE_COURS );
169         }
170 
171         addInfo( "import done" );
172 
173         return redirectView( request, VIEW_MANAGE_COURS );
174     }
175 
176 }