View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.genericattributes.modules.ocr.service;
35  
36  import java.util.List;
37  
38  import org.apache.commons.fileupload.FileItem;
39  
40  import fr.paris.lutece.plugins.genericattributes.business.IOcrProvider;
41  import fr.paris.lutece.plugins.genericattributes.business.Response;
42  import fr.paris.lutece.plugins.genericattributes.modules.ocr.exceptions.CallOcrException;
43  import fr.paris.lutece.plugins.genericattributes.modules.ocr.utils.OcrProviderUtils;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.util.ReferenceItem;
46  import fr.paris.lutece.util.ReferenceList;
47  
48  /**
49   *
50   * OcrProvider : provides ocr CNI support for Generic Attributes
51   *
52   */
53  public class OcrCniProvider implements IOcrProvider
54  {
55      private static final long serialVersionUID = 6224042984367506762L;
56      private static final String PROPERTY_KEY = "genericattributes-ocr.CNI.key";
57      private static final String PROPERTY_DISPLAYED_NAME = "genericattributes-ocr.CNI.displayName";
58      private static final String PROPERTY_AUTHORIZED_ENTRY_TYPE = "genericattributes-ocr.CNI.authorizedEntryType";
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public String getKey( )
65      {
66          return AppPropertiesService.getProperty( PROPERTY_KEY );
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      @Override
73      public String getDisplayedName( )
74      {
75          return AppPropertiesService.getProperty( PROPERTY_DISPLAYED_NAME );
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public ReferenceItem toRefItem( )
83      {
84          ReferenceItem refItem = new ReferenceItem( );
85  
86          refItem.setCode( getKey( ) );
87          refItem.setName( getDisplayedName( ) );
88  
89          return refItem;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public String toString( )
97      {
98          return "Ocr CNI Provider";
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public Object getParameter( int nKey )
106     {
107         // TODO Auto-generated method stub
108         return null;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public ReferenceList getListField( )
116     {
117         ReferenceList refListField = new ReferenceList( );
118 
119         refListField.addItem( 0, "First Name" );
120         refListField.addItem( 1, "Last Name" );
121         refListField.addItem( 2, "Birth date" );
122         refListField.addItem( 3, "Place of Birth" );
123         refListField.addItem( 4, "Expiration date" );
124         refListField.addItem( 5, "isuue date" );
125         refListField.addItem( 6, "Gender" );
126         refListField.addItem( 7, "Nationality" );
127         refListField.addItem( 8, "id number" );
128         refListField.addItem( 9, "Address" );
129 
130         return refListField;
131     }
132 
133     /**
134      * {@inheritDoc}
135      */
136     @Override
137     public ReferenceItem getFieldById( int idField )
138     {
139         return getListField( ).get( idField );
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     /*
146      * @Override public List<Integer> getAuthorizedEntryType() { String strAuthorizedEntryType = AppPropertiesService.getProperty(
147      * PROPERTY_AUTHORIZED_ENTRY_TYPE ); Pattern pattern = Pattern.compile("-"); return
148      * pattern.splitAsStream(strAuthorizedEntryType).map(Integer::valueOf).collect(Collectors.toList()); }
149      */
150 
151     /**
152      * {@inheritDoc}
153      * 
154      * @throws CallOcrException
155      */
156     @Override
157     public List<Response> process( FileItem fileUploaded, int nIdTargetEntry, String strResourceType ) throws CallOcrException
158     {
159         return OcrProviderUtils.process( fileUploaded, nIdTargetEntry, strResourceType, getKey( ), getListField( ) );
160     }
161 
162     @Override
163     public String getConfigHtmlCode( ReferenceList lisEntry, int nIdQuestion, String strResourceType )
164     {
165 
166         return OcrProviderUtils.builtTempalteConfiog( lisEntry, this, nIdQuestion, strResourceType ).getHtml( );
167     }
168 
169     @Override
170     public String getHtmlCode( int nIdTargetEntry, String strResourceType )
171     {
172 
173         return OcrProviderUtils.builtTempalteCode( nIdTargetEntry, strResourceType ).getHtml( );
174     }
175 }