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.formengine.service.output;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPropertiesService;
38  
39  import java.io.File;
40  import java.io.FileNotFoundException;
41  import java.io.IOException;
42  import java.io.RandomAccessFile;
43  
44  
45  /**
46   * This abstract class defines the basis for id generation from a file.
47   *
48   */
49  public abstract class FileIdGenerator extends IdGenerator
50  {
51      private String _strFileName;
52      private String _strDirectoryPathProperty;
53  
54      /**
55       * {@inheritDoc }
56       */
57      protected String loadId(  )
58      {
59          String strId = null;
60  
61          try
62          {
63              //            File file = new File( AppPathService.getPath( getDirectoryPathProperty(  ), getFileName(  ) ) );
64              File file = new File( AppPropertiesService.getProperty( getDirectoryPathProperty(  ) ) + getFileName(  ) );
65  
66              // FIXME return true to indicate a directory creation, false if directory exist
67              file.getParentFile(  ).mkdirs(  );
68  
69              // return true to indicate a file creation, false if file exist
70              boolean bNewFile = file.createNewFile(  );
71  
72              RandomAccessFile accessfile = new RandomAccessFile( file, "r" );
73  
74              if ( !bNewFile )
75              {
76                  strId = accessfile.readUTF(  );
77              }
78  
79              accessfile.close(  );
80          }
81          catch ( FileNotFoundException e )
82          {
83              // TODO Auto-generated catch block
84              AppLogService.error( e.getMessage(  ), e );
85          }
86          catch ( IOException e )
87          {
88              // TODO Auto-generated catch block
89              AppLogService.error( e.getMessage(  ), e );
90          }
91  
92          return strId;
93      }
94  
95      /**
96       * {@inheritDoc }
97       */
98      protected void storeId( String strId )
99      {
100         try
101         {
102             File file = new File( AppPropertiesService.getProperty( getDirectoryPathProperty(  ) ) + getFileName(  ) );
103 
104             // FIXME return true to indicate a directory creation, false if directory exist
105             file.getParentFile(  ).mkdirs(  );
106 
107             // return true to indicate a file creation, false if file exist
108             boolean bNewFile = file.createNewFile(  );
109 
110             RandomAccessFile accessfile = new RandomAccessFile( file, "rw" );
111 
112             if ( !bNewFile )
113             {
114                 accessfile.seek( 0 );
115                 accessfile.writeUTF( strId );
116             }
117 
118             accessfile.close(  );
119         }
120         catch ( FileNotFoundException e )
121         {
122             // TODO Auto-generated catch block
123             AppLogService.error( e.getMessage(  ), e );
124         }
125         catch ( IOException e )
126         {
127             // TODO Auto-generated catch block
128             AppLogService.error( e.getMessage(  ), e );
129         }
130     }
131 
132     /**
133      * Get the name of the file storing the id
134      * @return the filename
135      */
136     protected String getFileName(  )
137     {
138         return _strFileName;
139     }
140 
141     /**
142      * Set the name of the file storing the id
143      * @param strFileName the name of the id file
144      */
145     protected void setFileName( String strFileName )
146     {
147         _strFileName = strFileName;
148     }
149 
150     /**
151      * Get the property giving the path name
152      * the id is stored
153      * @return the property
154      */
155     protected String getDirectoryPathProperty(  )
156     {
157         return _strDirectoryPathProperty;
158     }
159 
160     /**
161      * Set the property giving the directory path name
162      * the id is stored
163      * @param strDirectoryPathProperty the property
164      */
165     protected void setDirectoryPathProperty( String strDirectoryPathProperty )
166     {
167         _strDirectoryPathProperty = strDirectoryPathProperty;
168     }
169 }