1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.pluginwizard.service;
35
36 import fr.paris.lutece.plugins.pluginwizard.business.model.PluginModel;
37 import fr.paris.lutece.plugins.pluginwizard.service.generator.GeneratorService;
38 import fr.paris.lutece.portal.service.plugin.Plugin;
39 import fr.paris.lutece.portal.service.plugin.PluginService;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41
42 import java.io.ByteArrayInputStream;
43 import java.io.ByteArrayOutputStream;
44
45 import java.util.Map;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipOutputStream;
48
49 import javax.servlet.http.HttpServletRequest;
50
51
52
53
54 public class PluginWizardZipService
55 {
56 private static PluginWizardZipService _singleton;
57 private static final String PARAM_PLUGIN_ID = "plugin_id";
58 private static final String PARAM_SCHEME = "scheme";
59 private static Plugin _plugin = PluginService.getPlugin( "pluginwizard" );
60
61
62
63
64
65
66 public static synchronized PluginWizardZipService getInstance( )
67 {
68 if ( _singleton == null )
69 {
70 _singleton = new PluginWizardZipService( );
71 }
72
73 return _singleton;
74 }
75
76
77
78
79
80
81
82
83 public byte [ ] exportZip( HttpServletRequest request )
84 {
85 ByteArrayOutputStream fos = new ByteArrayOutputStream( );
86
87 byte [ ] buffer = new byte [ 1024];
88
89 ZipOutputStream zipOutputStream = new ZipOutputStream( fos );
90 zipOutputStream.setLevel( 9 );
91
92 String strPluginId = request.getParameter( PARAM_PLUGIN_ID );
93 int nPluginId = Integer.parseInt( strPluginId );
94 String strScheme = request.getParameter( PARAM_SCHEME );
95 int nScheme = Integer.parseInt( strScheme );
96 PluginModel pluginModel = ModelService.getPluginModel( nPluginId );
97 GeneratorServiced/service/generator/GeneratorService.html#GeneratorService">GeneratorService generator = new GeneratorService( );
98 Map<String, String> mapSources = generator.getGeneratedSources( _plugin, pluginModel, nScheme );
99
100 try
101 {
102 for ( Map.Entry<String, String> sourceFile : mapSources.entrySet( ) )
103 {
104 StringBuilder sb = new StringBuilder( sourceFile.getValue( ) );
105 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( sb.toString( ).getBytes( "UTF-8" ) );
106 zipOutputStream.putNextEntry( new ZipEntry( sourceFile.getKey( ) ) );
107
108 int nLength;
109
110 while ( ( nLength = byteArrayInputStream.read( buffer ) ) > 0 )
111 {
112 zipOutputStream.write( buffer, 0, nLength );
113 }
114
115 zipOutputStream.closeEntry( );
116 byteArrayInputStream.close( );
117 }
118
119 zipOutputStream.finish( );
120 zipOutputStream.close( );
121 }
122 catch( Exception e )
123 {
124 AppLogService.error( e );
125 }
126
127 return fos.toByteArray( );
128 }
129
130 public String getPluginName( HttpServletRequest request ) {
131 String strPluginId = request.getParameter( PARAM_PLUGIN_ID );
132 int nPluginId = Integer.parseInt( strPluginId );
133 PluginModel pluginModel = ModelService.getPluginModel( nPluginId );
134 return pluginModel.isModule() ? pluginModel.getModuleName() : pluginModel.getPluginName();
135 }
136 }