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.EleveArpege;
18  import fr.paris.lutece.plugins.atelieraba.business.EleveArpegeHome;
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 Eleves features ( manage )
27   */
28  @Controller( controllerJsp = "ManageElevesArpege.jsp", controllerPath = "jsp/admin/plugins/atelieraba/", right = "ATELIERABA_ELEVES_MANAGEMENT" )
29  public class ElevesArpegeJspBean extends MVCAdminJspBean
30  {
31  
32      /**
33       *
34       */
35      private static final long   serialVersionUID                  = 5827962602778559532L;
36  
37      // Views
38      private static final String VIEW_MANAGE_ELEVES                = "manageEleves";
39      private static final String PROPERTY_PAGE_TITLE_MANAGE_ELEVES = "atelieraba.manage_eleves_arpege.pageTitle";
40      // Templates
41      private static final String TEMPLATE_MANAGE_ELEVES            = "/admin/plugins/atelieraba/manage_eleves_arpege.html";
42  
43      // Properties for page titles
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              = 4;
50  
51      // Actions
52      private static final String ACTION_IMPORT_COURS               = "import_eleves_arpege";
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_ELEVES, defaultView = true )
62      public String getManageEleves( HttpServletRequest request )
63      {
64  
65          Map<String, Object> model = getModel( );
66  
67          return getPage( PROPERTY_PAGE_TITLE_MANAGE_ELEVES, TEMPLATE_MANAGE_ELEVES, model );
68  
69      }
70  
71      /**
72       * Process the import/export
73       *
74       * @param request
75       *            The Http Request
76       * @return The Jsp URL of the process result
77       */
78      /**
79       * @param request
80       * @return
81       */
82      @Action( ACTION_IMPORT_COURS )
83      public String doImport( HttpServletRequest request )
84      {
85          MultipartHttpServletRequest multipartRequest = ( MultipartHttpServletRequest ) request;
86          FileItem fileSource = multipartRequest.getFile( "file" );
87          CSVReader reader;
88  
89          try
90          {
91              BufferedInputStream bis = new BufferedInputStream( fileSource.getInputStream( ) );
92              CharsetDetector cd = new CharsetDetector( );
93              cd.setText( bis );
94              CharsetMatch cm = cd.detect( );
95              String charset = cm.getName( );
96              String filename = fileSource.getName( );
97  
98              if ( !filename.endsWith( ".csv" ) )
99              {
100                 addError( ERROR_IMPORT_BAD_EXTENSION_FILE, getLocale( ) );
101 
102                 return redirectView( request, VIEW_MANAGE_ELEVES );
103             }
104 
105             reader = new CSVReader( new InputStreamReader( fileSource.getInputStream( ), charset ), ';' );
106             String[] header = reader.readNext( );
107 
108             if ( header.length != EXPECTED_LINE_LENGTH )
109             {
110                 addError( ERROR_IMPORT_BAD_LENGTH_HEADER, getLocale( ) );
111                 addError( EXPECTED_LINE_LENGTH + "" );
112 
113                 return redirectView( request, VIEW_MANAGE_ELEVES );
114             }
115 
116             String[] nextLine;
117             int lineNumber = 1;
118 
119             while ( ( nextLine = reader.readNext( ) ) != null )
120             {
121 
122                 lineNumber++;
123 
124                 if ( nextLine.length != EXPECTED_LINE_LENGTH )
125                 {
126                     addError( ERROR_IMPORT_BAD_FILE, getLocale( ) );
127                     addError( lineNumber + "" );
128 
129                     return redirectView( request, VIEW_MANAGE_ELEVES );
130                 }
131 
132                 for ( String eleve : nextLine )
133                 {
134                     if ( StringUtils.isNotBlank( eleve ) )
135                     {
136                         EleveArpege eleveExistant = EleveArpegeHome.findByCode( nextLine[0], nextLine[1], nextLine[2], nextLine[3] );
137 
138                         if ( eleveExistant == null )
139                         {
140                             EleveArpege eleveArpege = new EleveArpege( );
141                             eleveArpege.setNom( nextLine[0] );
142                             eleveArpege.setPrenom( nextLine[1] );
143                             eleveArpege.setTelephoneContact( nextLine[2].trim( ) );
144                             eleveArpege.setEmailContact( nextLine[3] );
145                             EleveArpegeHome.create( eleveArpege );
146                         }
147                         break;
148                     }
149 
150                 }
151             }
152             reader.close( );
153         } catch ( FileNotFoundException e )
154         {
155             addError( ERROR_IMPORT_BAD_FILENOTFOUND, getLocale( ) );
156             addError( e.toString( ) );
157 
158             return redirectView( request, VIEW_MANAGE_ELEVES );
159         } catch ( IOException e )
160         {
161             addError( ERROR_IMPORT_BAD_UNKNOWN, getLocale( ) );
162             addError( e.toString( ) );
163 
164             return redirectView( request, VIEW_MANAGE_ELEVES );
165         }
166 
167         addInfo( "import done" );
168 
169         return redirectView( request, VIEW_MANAGE_ELEVES );
170     }
171 
172 }