Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
203 | jmachado | 1 | package pt.estgp.estgweb.services.data; |
2 | |||
1814 | jmachado | 3 | import org.apache.log4j.Logger; |
332 | jmachado | 4 | import pt.estgp.estgweb.domain.*; |
203 | jmachado | 5 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
1814 | jmachado | 6 | import pt.estgp.estgweb.domain.typesgen.repository.AbstractRepositorySourceModuleInterfacePlugin; |
203 | jmachado | 7 | import pt.estgp.estgweb.filters.chains.ResourceAccessControlEnum; |
8 | import pt.estgp.estgweb.services.data.impl.DefaultRepositoryFile; |
||
1814 | jmachado | 9 | import pt.estgp.estgweb.utils.DatesWebUtils; |
1830 | jmachado | 10 | import pt.estgp.estgweb.utils.Globals; |
11 | import pt.estgp.estgweb.web.controllers.utils.FileUploaded; |
||
730 | jmachado | 12 | import pt.utl.ist.berserk.logic.serviceManager.IService; |
203 | jmachado | 13 | |
1830 | jmachado | 14 | import java.io.*; |
730 | jmachado | 15 | import java.util.ArrayList; |
203 | jmachado | 16 | import java.util.Date; |
218 | jmachado | 17 | import java.util.List; |
203 | jmachado | 18 | |
19 | /** |
||
20 | * @author Jorge Machado |
||
21 | * @date 4/Jun/2008 |
||
22 | * @see pt.estgp.estgweb.services.data |
||
23 | */ |
||
24 | public class RepositoryService implements IService |
||
25 | { |
||
26 | |||
1814 | jmachado | 27 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(RepositoryService.class); |
28 | |||
1830 | jmachado | 29 | |
30 | /************************************************* |
||
31 | * |
||
32 | * SERVICOS DO MODULO DE UTILITARIOS WEB |
||
33 | * TRANSACIONAL ISOLADOS |
||
34 | * |
||
35 | * ***********************************************/ |
||
36 | |||
37 | /** |
||
38 | * |
||
39 | * @param fileUploaded |
||
40 | * @param accessControl |
||
41 | * @param userSession |
||
42 | * @return |
||
43 | * @throws FileNotFoundException |
||
44 | */ |
||
45 | public RepositoryFile4JsonView storeRepositoryFileFromFileUpload(FileUploaded fileUploaded, ResourceAccessControlEnum accessControl, UserSession userSession) throws 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException |
||
46 | { |
||
47 | 1.5.0/docs/api/java/io/File.html">File tmpFile = new 1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + 1.5.0/docs/api/java/io/File.html">File.separator + fileUploaded.getTmpName()); |
||
48 | 1.5.0/docs/api/java/lang/String.html">String identifier = storeRepositoryFile(new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(tmpFile), |
||
49 | fileUploaded.getContentType(), |
||
50 | fileUploaded.getExtension(), |
||
51 | (int)fileUploaded.getFileSize(), |
||
52 | fileUploaded.getFileName(), |
||
53 | fileUploaded.getDescription(), |
||
54 | accessControl, |
||
55 | null, |
||
56 | userSession.getUser()); |
||
57 | RepositoryFile4JsonView repositoryFile4JsonView = new RepositoryFile4JsonView(); |
||
58 | repositoryFile4JsonView.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
59 | repositoryFile4JsonView.setExtension(fileUploaded.getExtension()); |
||
60 | repositoryFile4JsonView.setName(fileUploaded.getFileName()); |
||
61 | repositoryFile4JsonView.setContentType(fileUploaded.getContentType()); |
||
62 | repositoryFile4JsonView.setIdentifier(identifier); |
||
63 | repositoryFile4JsonView.setSize((int) fileUploaded.getFileSize()); |
||
64 | return repositoryFile4JsonView; |
||
65 | |||
66 | } |
||
67 | |||
68 | public RepositoryFile4JsonView replaceRepositoryFileFromFileUpload(1.5.0/docs/api/java/lang/String.html">String identifier, FileUploaded fileUploaded, ResourceAccessControlEnum accessControl, UserSession userSession) throws 1.5.0/docs/api/java/io/IOException.html">IOException |
||
69 | { |
||
70 | 1.5.0/docs/api/java/io/File.html">File tmpFile = new 1.5.0/docs/api/java/io/File.html">File(Globals.TMP_DIR + 1.5.0/docs/api/java/io/File.html">File.separator + fileUploaded.getTmpName()); |
||
71 | replaceLastRepositoryFileVersion(identifier,new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(tmpFile), |
||
72 | fileUploaded.getContentType(), |
||
73 | fileUploaded.getExtension(), |
||
74 | (int)fileUploaded.getFileSize(), |
||
75 | fileUploaded.getFileName(), |
||
76 | fileUploaded.getDescription(), |
||
77 | accessControl); |
||
78 | RepositoryFile4JsonView repositoryFile4JsonView = new RepositoryFile4JsonView(); |
||
79 | repositoryFile4JsonView.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
80 | repositoryFile4JsonView.setExtension(fileUploaded.getExtension()); |
||
81 | repositoryFile4JsonView.setName(fileUploaded.getFileName()); |
||
82 | repositoryFile4JsonView.setContentType(fileUploaded.getContentType()); |
||
83 | repositoryFile4JsonView.setIdentifier(identifier); |
||
84 | repositoryFile4JsonView.setSize((int) fileUploaded.getFileSize()); |
||
85 | return repositoryFile4JsonView; |
||
86 | } |
||
87 | |||
88 | |||
89 | /************************************************* |
||
90 | * |
||
91 | * SERVICOS DO REPOSITORIO NAO TRANSACIONAIS ISOLADOS |
||
92 | * |
||
93 | * ***********************************************/ |
||
94 | |||
95 | |||
96 | /** |
||
97 | * * Layer 1 |
||
98 | * @param stream |
||
99 | * @param contentType |
||
100 | * @param extension |
||
101 | * @param size |
||
102 | * @param name |
||
103 | * @param description |
||
104 | * @param accessControl |
||
105 | * @param sourceModuleInterfacePlugin |
||
106 | * @param userSession |
||
107 | * @return |
||
108 | */ |
||
1703 | jmachado | 109 | public 1.5.0/docs/api/java/lang/String.html">String storeRepositoryFile(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String contentType, 1.5.0/docs/api/java/lang/String.html">String extension, int size, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String description, ResourceAccessControlEnum accessControl, AbstractRepositorySourceModuleInterfacePlugin sourceModuleInterfacePlugin, UserSession userSession) |
730 | jmachado | 110 | { |
1703 | jmachado | 111 | return storeRepositoryFile(stream,contentType,extension,size,name,description,accessControl, sourceModuleInterfacePlugin, userSession.getUser()); |
730 | jmachado | 112 | } |
203 | jmachado | 113 | /** |
1830 | jmachado | 114 | * Guarda um novo Repository File |
335 | jmachado | 115 | * @param stream . |
116 | * @param contentType . |
||
1703 | jmachado | 117 | * @param extension . |
335 | jmachado | 118 | * @param size . |
119 | * @param name . |
||
120 | * @param description . |
||
121 | * @param accessControl . |
||
1703 | jmachado | 122 | * @param sourceModuleInterfacePlugin |
123 | *@param user . @return the identifier to retrieve the document |
||
203 | jmachado | 124 | */ |
1703 | jmachado | 125 | public 1.5.0/docs/api/java/lang/String.html">String storeRepositoryFile(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String contentType, 1.5.0/docs/api/java/lang/String.html">String extension, int size, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String description, ResourceAccessControlEnum accessControl, AbstractRepositorySourceModuleInterfacePlugin sourceModuleInterfacePlugin, User user) |
203 | jmachado | 126 | { |
127 | RepositoryFileImpl repositoryFile = DomainObjectFactory.createRepositoryFileImpl(); |
||
128 | repositoryFile.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
215 | jmachado | 129 | repositoryFile.setDeleted(false); |
203 | jmachado | 130 | repositoryFile.setAccessControl(accessControl.getKey()); |
730 | jmachado | 131 | repositoryFile.setOwner(user); |
1703 | jmachado | 132 | repositoryFile.setSourceModule(sourceModuleInterfacePlugin); |
203 | jmachado | 133 | DaoFactory.getRepositoryFileDaoImpl().save(repositoryFile); |
1830 | jmachado | 134 | RepositoryServiceTier2.storeNewVersion(stream, contentType, extension, size, name, description, repositoryFile); |
135 | //returning id |
||
1814 | jmachado | 136 | return new 1.5.0/docs/api/java/lang/StringBuilder.html">StringBuilder().append(repositoryFile.getId()).toString(); |
137 | } |
||
138 | |||
1830 | jmachado | 139 | /** |
140 | * Manda substituir a versão do ficheiro no layer 2 sem criar novo |
||
141 | * substituindo o ficheiro |
||
142 | * @param stream . |
||
143 | * @param contentType . |
||
144 | * @param extension . |
||
145 | * @param size . |
||
146 | * @param name . |
||
147 | * @param description . |
||
148 | * @param accessControl . |
||
149 | |||
150 | */ |
||
151 | public void replaceLastRepositoryFileVersion(1.5.0/docs/api/java/lang/String.html">String identifier, 5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String contentType, 1.5.0/docs/api/java/lang/String.html">String extension, int size, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String description, ResourceAccessControlEnum accessControl) |
||
152 | { |
||
153 | RepositoryFileImpl repositoryFile = (RepositoryFileImpl) DaoFactory.getRepositoryFileDaoImpl().get(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
||
154 | repositoryFile.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
155 | repositoryFile.setAccessControl(accessControl.getKey()); |
||
156 | RepositoryServiceTier2.replaceLastVersion(stream, contentType, extension, size, name, description, repositoryFile); |
||
203 | jmachado | 157 | } |
158 | |||
215 | jmachado | 159 | /** |
1830 | jmachado | 160 | * Manda criar uma nova versão do ficheiro no layer 2 |
335 | jmachado | 161 | * @param stream . |
332 | jmachado | 162 | * @param contentType . |
335 | jmachado | 163 | * @param size . |
164 | * @param name . |
||
165 | * @param description . |
||
166 | * @param identifier . |
||
167 | * @param extension . |
||
332 | jmachado | 168 | * @param accessControl . |
1830 | jmachado | 169 | * @return new file version id |
203 | jmachado | 170 | */ |
1830 | jmachado | 171 | public long updateRepositoryFile(1.5.0/docs/api/java/lang/String.html">String identifier, 5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/lang/String.html">String contentType, 1.5.0/docs/api/java/lang/String.html">String extension, int size, 1.5.0/docs/api/java/lang/String.html">String name, 1.5.0/docs/api/java/lang/String.html">String description, ResourceAccessControlEnum accessControl) |
203 | jmachado | 172 | { |
173 | |||
204 | jmachado | 174 | RepositoryFileImpl repositoryFile = (RepositoryFileImpl) DaoFactory.getRepositoryFileDaoImpl().get(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
203 | jmachado | 175 | repositoryFile.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
176 | repositoryFile.setAccessControl(accessControl.getKey()); |
||
1830 | jmachado | 177 | return RepositoryServiceTier2.storeNewVersion(stream, contentType, extension, size, name, description, repositoryFile); |
203 | jmachado | 178 | } |
179 | |||
215 | jmachado | 180 | |
1830 | jmachado | 181 | |
182 | |||
183 | |||
184 | |||
185 | |||
186 | |||
187 | |||
188 | |||
189 | /**LAYER 1 READ SERVICES**/ |
||
190 | |||
191 | |||
192 | |||
193 | |||
203 | jmachado | 194 | /** |
332 | jmachado | 195 | * @param identifier . |
203 | jmachado | 196 | * @return a view of a document |
197 | */ |
||
198 | public RepositoryFileImpl loadView(1.5.0/docs/api/java/lang/String.html">String identifier) |
||
199 | { |
||
204 | jmachado | 200 | RepositoryFileImpl repositoryFile = (RepositoryFileImpl) DaoFactory.getRepositoryFileDaoImpl().get(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
335 | jmachado | 201 | for (RepositoryFileVersionImpl version : repositoryFile.getRepositoryFileVersions()) |
203 | jmachado | 202 | { |
203 | version.getSaveDate(); |
||
204 | } |
||
205 | return repositoryFile; |
||
206 | } |
||
207 | |||
760 | jmachado | 208 | |
209 | public RepositoryFileImpl loadView(1.5.0/docs/api/java/lang/String.html">String identifier,UserSession sess) |
||
1830 | jmachado | 210 | { |
211 | return loadView(identifier); |
||
212 | } |
||
760 | jmachado | 213 | |
1830 | jmachado | 214 | |
215 | |||
1106 | jmachado | 216 | public IRepositoryFile loadByIdentifier(1.5.0/docs/api/java/lang/String.html">String identifier) |
1103 | jmachado | 217 | { |
218 | return load(identifier,null); |
||
219 | } |
||
1670 | jmachado | 220 | |
203 | jmachado | 221 | /** |
1670 | jmachado | 222 | * load a INputStream view of the last version only |
223 | * @param identifier |
||
203 | jmachado | 224 | * @return a view with an inputStream for document |
225 | */ |
||
1670 | jmachado | 226 | |
1675 | jmachado | 227 | |
335 | jmachado | 228 | public IRepositoryFile load(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession userSession) |
203 | jmachado | 229 | { |
230 | DefaultRepositoryFile iRepositoryFile = new DefaultRepositoryFile(); |
||
231 | RepositoryFileImpl repositoryFile = (RepositoryFileImpl) DaoFactory.getRepositoryFileDaoImpl().load(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
||
232 | iRepositoryFile.setContentType(repositoryFile.getLastVersion().getContentType()); |
||
233 | iRepositoryFile.setSize(repositoryFile.getLastVersion().getSize()); |
||
234 | iRepositoryFile.setName(repositoryFile.getLastVersion().getName()); |
||
1814 | jmachado | 235 | |
1586 | jmachado | 236 | iRepositoryFile.setExtension(repositoryFile.getLastVersion().getExtension()); |
1802 | jmachado | 237 | iRepositoryFile.setSaveDate(repositoryFile.getLastVersion().getSaveDate()); |
1814 | jmachado | 238 | |
1830 | jmachado | 239 | iRepositoryFile.setInput(RepositoryServiceTier3.getPhisicalInputStreamCheckMirrors(repositoryFile.getLastVersion())); |
1814 | jmachado | 240 | |
203 | jmachado | 241 | return iRepositoryFile; |
242 | } |
||
243 | |||
1814 | jmachado | 244 | |
245 | |||
246 | /** |
||
335 | jmachado | 247 | * @param identifier . |
248 | * @param userSession . |
||
218 | jmachado | 249 | * @return a view with an inputStream for document |
250 | */ |
||
760 | jmachado | 251 | public IRepositoryFile loadVersion(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession userSession) |
252 | { |
||
253 | DefaultRepositoryFile iRepositoryFile = new DefaultRepositoryFile(); |
||
254 | RepositoryFileVersionImpl repositoryFile = (RepositoryFileVersionImpl) DaoFactory.getRepositoryFileVersionDaoImpl().load(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
||
255 | iRepositoryFile.setContentType(repositoryFile.getContentType()); |
||
256 | iRepositoryFile.setSize(repositoryFile.getSize()); |
||
257 | iRepositoryFile.setName(repositoryFile.getName()); |
||
1830 | jmachado | 258 | iRepositoryFile.setInput(RepositoryServiceTier3.getPhisicalInputStreamCheckMirrors(repositoryFile)); |
1586 | jmachado | 259 | iRepositoryFile.setExtension(repositoryFile.getExtension()); |
1802 | jmachado | 260 | iRepositoryFile.setSaveDate(repositoryFile.getSaveDate()); |
760 | jmachado | 261 | return iRepositoryFile; |
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param identifier . |
||
266 | * @param userSession . |
||
267 | * @return a view with an inputStream for document |
||
268 | */ |
||
335 | jmachado | 269 | public List<IRepositoryFile> loadVersions(1.5.0/docs/api/java/lang/String.html">String identifier, UserSession userSession) |
218 | jmachado | 270 | { |
271 | RepositoryFileImpl repositoryFile = (RepositoryFileImpl) DaoFactory.getRepositoryFileDaoImpl().load(1.5.0/docs/api/java/lang/Long.html">Long.parseLong(identifier)); |
||
272 | List<IRepositoryFile> versions = new ArrayList<IRepositoryFile>(); |
||
335 | jmachado | 273 | for (RepositoryFileVersionImpl version : repositoryFile.getRepositoryFileVersions()) |
218 | jmachado | 274 | { |
275 | DefaultRepositoryFile iRepositoryFile = new DefaultRepositoryFile(); |
||
276 | iRepositoryFile.setContentType(version.getContentType()); |
||
277 | iRepositoryFile.setSize(version.getSize()); |
||
1814 | jmachado | 278 | iRepositoryFile.setName(DatesWebUtils.getStringFromDateForFilename(version.getSaveDate()) + "-" + version.getName()); |
1830 | jmachado | 279 | iRepositoryFile.setInput(RepositoryServiceTier3.getPhisicalInputStreamCheckMirrors(version)); |
1586 | jmachado | 280 | iRepositoryFile.setExtension(version.getExtension()); |
1802 | jmachado | 281 | iRepositoryFile.setSaveDate(version.getSaveDate()); |
218 | jmachado | 282 | versions.add(iRepositoryFile); |
283 | } |
||
284 | return versions; |
||
285 | } |
||
286 | |||
332 | jmachado | 287 | |
288 | |||
1703 | jmachado | 289 | |
290 | |||
291 | |||
334 | jmachado | 292 | |
293 | |||
1703 | jmachado | 294 | |
295 | |||
1814 | jmachado | 296 | |
297 | |||
334 | jmachado | 298 | |
299 | |||
1830 | jmachado | 300 | /* |
1814 | jmachado | 301 | public static void main(String[] args) throws Exception { |
302 | AbstractDao.getCurrentSession().beginTransaction(); |
||
303 | String fileName = "/Volumes/Home/jorgemachado/Downloads/CV_PedroPepe_Docente4.pdf"; |
||
304 | new RepositoryService().storeRepositoryFile( |
||
305 | new FileInputStream(fileName), |
||
306 | MimeTypeGuesser.getInstance().guessMimeType(fileName), |
||
307 | FilesUtils.getExtension(fileName), (int) (new java.io.File(fileName)).length(),"CV_PedroPepe_Docente4.pdf","teste",ResourceAccessControlEnum.publicDomain,null,DaoFactory.getUserDaoImpl().load(new Long(1))); |
||
308 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
309 | |||
310 | System.out.println(FilesUtils.getMD5Checksum(new java.io.File("/Volumes/Home/jorgemachado/Downloads/CV_PedroPepe_Docente4.pdf"))); |
||
1830 | jmachado | 311 | }*/ |
1814 | jmachado | 312 | |
313 | |||
203 | jmachado | 314 | } |