~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to local/vn/dhis-web-hue-reporttool/src/main/java/org/hisp/dhis/rt/utils/FileUtils.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.rt.utils;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
7
 * Redistribution and use in source and binary forms, with or without
 
8
 * modification, are permitted provided that the following conditions are met:
 
9
 * * Redistributions of source code must retain the above copyright notice, this
 
10
 *   list of conditions and the following disclaimer.
 
11
 * * Redistributions in binary form must reproduce the above copyright notice,
 
12
 *   this list of conditions and the following disclaimer in the documentation
 
13
 *   and/or other materials provided with the distribution.
 
14
 * * Neither the name of the HISP project nor the names of its contributors may
 
15
 *   be used to endorse or promote products derived from this software without
 
16
 *   specific prior written permission.
 
17
 *
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 
21
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 
22
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 
25
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 */
 
29
 
 
30
import java.io.BufferedReader;
 
31
import java.io.File;
 
32
import java.io.FileNotFoundException;
 
33
import java.io.FileOutputStream;
 
34
import java.io.IOException;
 
35
import java.io.InputStream;
 
36
import java.io.InputStreamReader;
 
37
import java.io.OutputStreamWriter;
 
38
import java.io.UnsupportedEncodingException;
 
39
import java.io.Writer;
 
40
import java.util.HashMap;
 
41
import java.util.Map;
 
42
 
 
43
import javax.servlet.ServletContext;
 
44
 
 
45
import com.opensymphony.webwork.ServletActionContext;
 
46
 
 
47
/**
 
48
 * @author Lars Helge Overland
 
49
 * @author Nguyen Dang Quang
 
50
 * @version $Id: FileUtils.java 2871 2007-02-20 16:04:11Z andegje $
 
51
 */
 
52
public class FileUtils
 
53
{
 
54
    /**
 
55
     * Creates the dhis directory tree with the user.home directory as root. Valid 
 
56
     * directories are "dhis", "rt", "design", "xml", "jrxml", "reports", "pdf" and "html".
 
57
     * @return Map with the paths to all directories
 
58
     */
 
59
    public static Map getOutputPath()
 
60
    {
 
61
        Map<String, String> paths = new HashMap<String, String>();
 
62
        
 
63
        String dhis_dir = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator;
 
64
        String rt_dir = dhis_dir + "rt" + File.separator;
 
65
        String design_dir = rt_dir + "design" + File.separator;
 
66
        String xml_dir = design_dir + "xml" + File.separator;
 
67
        String jrxml_dir = design_dir + "jrxml" + File.separator;
 
68
        String reports_dir = rt_dir + "reports" + File.separator;
 
69
        String pdf_dir = reports_dir + "PDF" + File.separator;
 
70
        String html_dir = reports_dir + "HTML" + File.separator;
 
71
        
 
72
        paths.put( "dhis", dhis_dir );
 
73
        paths.put( "rt", rt_dir );
 
74
        paths.put( "design", design_dir );
 
75
        paths.put( "xml", xml_dir );
 
76
        paths.put( "jrxml", jrxml_dir );
 
77
        paths.put( "reports", reports_dir );
 
78
        paths.put( "pdf", pdf_dir );
 
79
        paths.put( "html", html_dir );
 
80
        
 
81
        File dhisDir = new File( dhis_dir );
 
82
        if ( !dhisDir.isDirectory() )
 
83
        {
 
84
            dhisDir.mkdir();
 
85
        }
 
86
        File rtDir = new File( rt_dir );
 
87
        if ( !rtDir.isDirectory() )
 
88
        {
 
89
            rtDir.mkdir();
 
90
        }
 
91
        File designDir = new File( design_dir );
 
92
        if ( !designDir.isDirectory() )
 
93
        {
 
94
            designDir.mkdir();
 
95
        }
 
96
        File xmlDir = new File( xml_dir );
 
97
        if ( !xmlDir.isDirectory() )
 
98
        {
 
99
            xmlDir.mkdir();
 
100
        }
 
101
        File jrxmlDir = new File( jrxml_dir );
 
102
        if ( !jrxmlDir.isDirectory() )
 
103
        {
 
104
            jrxmlDir.mkdir();
 
105
        }
 
106
        File reportsDir = new File( reports_dir );
 
107
        if ( !reportsDir.isDirectory() )
 
108
        {
 
109
            reportsDir.mkdir();
 
110
        }
 
111
        File pdfDir = new File( pdf_dir );
 
112
        if ( !pdfDir.isDirectory() )
 
113
        {
 
114
            pdfDir.mkdir();
 
115
        }
 
116
        File htmlDir = new File( html_dir );
 
117
        if ( !htmlDir.isDirectory() )
 
118
        {
 
119
            htmlDir.mkdir();
 
120
        }
 
121
        
 
122
        checkXmlFile( xml_dir + "Hue_bieu_CoverSignature.xml" );
 
123
                
 
124
        checkXmlFile( xml_dir + "Hue_bieu_1.xml" );
 
125
        
 
126
        checkXmlFile( xml_dir + "Hue_bieu_2.xml" );
 
127
                
 
128
        checkXmlFile( xml_dir + "Hue_bieu_3.xml" );
 
129
                
 
130
        checkXmlFile( xml_dir + "Hue_bieu_4.xml" );
 
131
                
 
132
        checkXmlFile( xml_dir + "Hue_bieu_5.xml" );
 
133
                
 
134
        checkXmlFile( xml_dir + "Hue_bieu_7.xml" );
 
135
                
 
136
        checkXmlFile( xml_dir + "Hue_bieu_9.xml" );
 
137
        
 
138
        checkXmlFile( xml_dir + "Hue_bieu_10.xml" );
 
139
 
 
140
        checkXmlFile( xml_dir + "Hue_bieu_11.xml" );
 
141
 
 
142
        checkXmlFile( xml_dir + "Hue_bieu_12.xml" );
 
143
                
 
144
        checkXmlFile( xml_dir + "Hue_bieu_13.xml" );
 
145
 
 
146
        checkXmlFile( xml_dir + "Hue_bieu_14.xml" );
 
147
                
 
148
        return paths;
 
149
    }
 
150
    
 
151
    /**
 
152
     * Creates a valid Java class name by removing illegal characters.
 
153
     * @param input Input string
 
154
     * @return Valid Java class name string
 
155
     */
 
156
    public static String getQualifiedFileName( String name )
 
157
    {    
 
158
        String pattern = "[ (){}\\[\\]\\'\\+\\*\\/\\-\\\\]";
 
159
        String replacement = "";        
 
160
        name = name.replaceAll( pattern, replacement );        
 
161
        
 
162
        return name;
 
163
    }
 
164
    
 
165
    /**
 
166
     * Gets the extension of a file.
 
167
     * @param file The file
 
168
     * @return The extension of the file
 
169
     */
 
170
    public static String getExtension( File file )
 
171
    {
 
172
        if ( file.exists() )
 
173
        {
 
174
            String path = file.getName();
 
175
            return path.substring( path.lastIndexOf( "." ) + 1, path.length() );
 
176
        }
 
177
        
 
178
        return null;
 
179
    }
 
180
    
 
181
    /**
 
182
     * Gets the extension of a file.
 
183
     * @param fileName The file name
 
184
     * @return The extension of the file
 
185
     */
 
186
    public static String getExtension( String fileName )
 
187
    {
 
188
        return fileName.substring( fileName.lastIndexOf( "." ) + 1, fileName.length() );
 
189
    }
 
190
    
 
191
    /**
 
192
     * Gets the basename of a file.
 
193
     * @param file The file
 
194
     * @return The basename of the file
 
195
     */
 
196
    public static String getBaseName( File file )
 
197
    {
 
198
        if ( file.exists() )
 
199
        {
 
200
            String path = file.getName();
 
201
            return path.substring( 0 , path.lastIndexOf( "." ) );
 
202
        }
 
203
        
 
204
        return null;
 
205
    }
 
206
    
 
207
    /**
 
208
     * Gets the basename of a file.
 
209
     * @param file The file name
 
210
     * @return The basename of the file
 
211
     */    
 
212
    public static String getBaseName( String fileName )
 
213
    {
 
214
        return fileName.substring( 0 , fileName.lastIndexOf( "." ) );
 
215
    }
 
216
    
 
217
    /**
 
218
     * Gets the path to the report xml file
 
219
     * @param name The file name
 
220
     * @return Path to the report xml file
 
221
     */
 
222
    public static String getXmlFileName( String name )
 
223
    {
 
224
        return getOutputPath().get( "xml" ).toString() + getQualifiedFileName( name ) + ".xml";
 
225
    }
 
226
    
 
227
    /**
 
228
     * Gets the path to the report jrxml file
 
229
     * @param name The file name
 
230
     * @return Path to the report jrxml file
 
231
     */
 
232
    public static String getJRXmlFileName( String name )
 
233
    {
 
234
        return getOutputPath().get( "jrxml" ).toString() + getQualifiedFileName( name ) + ".jrxml";
 
235
    }
 
236
    
 
237
    /*
 
238
     * Viet Nam only
 
239
     */
 
240
    public static InputStream getHcmcJRXmlFileAsStream( String kind, int page )
 
241
    {
 
242
        String path = "/dhis-web-reporttool/template/vietnam/" + kind + "/" + "Trang" + "_" + page + ".jrxml";
 
243
        ServletContext context = ServletActionContext.getServletContext();
 
244
        return context.getResourceAsStream( path );
 
245
    }
 
246
  
 
247
    public static InputStream getHcmcJasperFileAsStream( String kind, int page )
 
248
    {
 
249
        String path = "/dhis-web-reporttool/template/hcmc/" + kind + "/" + "Trang" + "_" + page + ".jasper";
 
250
 
 
251
        ServletContext context = ServletActionContext.getServletContext();
 
252
        System.out.println("chay toi day ko?");
 
253
        InputStream abc = context.getResourceAsStream( path );
 
254
        System.out.println("Sau khi doc jasper file");
 
255
        if (abc != null)System.out.println("stream is not null");
 
256
        return abc;
 
257
    }
 
258
 
 
259
    private static void makeXmlFile( String outputXmlFilePath )
 
260
    {
 
261
        String xmlFileName = outputXmlFilePath.substring(outputXmlFilePath.lastIndexOf(File.separator) + 1, outputXmlFilePath.length());
 
262
        
 
263
        try
 
264
        {
 
265
            Writer out = new OutputStreamWriter( new FileOutputStream( outputXmlFilePath, true ), "UTF8" );
 
266
            InputStreamReader streamReader = new InputStreamReader( getXmlFile( xmlFileName ), "UTF8" );
 
267
            BufferedReader bufferedReader = new BufferedReader( streamReader );
 
268
 
 
269
            StringBuffer buffer = new StringBuffer();
 
270
            String line = null;
 
271
            while ( (line = bufferedReader.readLine()) != null )
 
272
            {
 
273
                buffer.append( line + "\n" );
 
274
            }
 
275
 
 
276
            out.write( buffer.toString() );
 
277
            out.flush();
 
278
            out.close();
 
279
        }
 
280
        catch ( UnsupportedEncodingException e )
 
281
        {
 
282
            e.printStackTrace();
 
283
        }
 
284
        catch ( FileNotFoundException e )
 
285
        {
 
286
            e.printStackTrace();
 
287
        }
 
288
        catch ( IOException e )
 
289
        {
 
290
            e.printStackTrace();
 
291
        }
 
292
    }
 
293
 
 
294
    private static InputStream getXmlFile( String fileName )
 
295
    {
 
296
        String path = "/dhis-web-reporttool/template/vietnam/xml/" + fileName;
 
297
        ServletContext context = ServletActionContext.getServletContext();
 
298
        return context.getResourceAsStream( path );
 
299
    }
 
300
 
 
301
    public static InputStream getSignatureJrxmlAsStream()
 
302
    {
 
303
        String path = "/dhis-web-reporttool/template/vietnam/TrangChuKy.jrxml";
 
304
        ServletContext context = ServletActionContext.getServletContext();
 
305
        return context.getResourceAsStream( path );
 
306
    }
 
307
 
 
308
    private static void checkXmlFile( String xmlPath )
 
309
    {
 
310
        File xml_file = new File( xmlPath );
 
311
        if ( !xml_file.exists() )
 
312
        {
 
313
            makeXmlFile( xmlPath );
 
314
        }
 
315
    }
 
316
 
 
317
}
 
318
 
 
319
 
 
320