001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.documentlibrary.util;
016    
017    import com.liferay.documentlibrary.DuplicateDirectoryException;
018    import com.liferay.documentlibrary.DuplicateFileException;
019    import com.liferay.documentlibrary.NoSuchDirectoryException;
020    import com.liferay.documentlibrary.NoSuchFileException;
021    import com.liferay.documentlibrary.model.FileModel;
022    import com.liferay.portal.jcr.JCRConstants;
023    import com.liferay.portal.jcr.JCRFactory;
024    import com.liferay.portal.jcr.JCRFactoryUtil;
025    import com.liferay.portal.kernel.exception.PortalException;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedInputStream;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.search.Document;
031    import com.liferay.portal.kernel.search.Indexer;
032    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
033    import com.liferay.portal.kernel.search.SearchEngineUtil;
034    import com.liferay.portal.kernel.search.SearchException;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.StringPool;
037    import com.liferay.portal.kernel.util.StringUtil;
038    import com.liferay.portal.kernel.util.Validator;
039    import com.liferay.portal.service.ServiceContext;
040    import com.liferay.portal.util.PropsValues;
041    
042    import java.io.InputStream;
043    
044    import java.util.ArrayList;
045    import java.util.Calendar;
046    import java.util.Collection;
047    import java.util.Date;
048    import java.util.List;
049    
050    import javax.jcr.Node;
051    import javax.jcr.NodeIterator;
052    import javax.jcr.PathNotFoundException;
053    import javax.jcr.Property;
054    import javax.jcr.RepositoryException;
055    import javax.jcr.Session;
056    import javax.jcr.version.Version;
057    import javax.jcr.version.VersionHistory;
058    import javax.jcr.version.VersionIterator;
059    
060    import org.apache.commons.lang.StringUtils;
061    
062    /**
063     * @author Michael Young
064     * @author Brian Wing Shun Chan
065     */
066    public class JCRHook extends BaseHook {
067    
068            public void addDirectory(long companyId, long repositoryId, String dirName)
069                    throws PortalException, SystemException {
070    
071                    Session session = null;
072    
073                    try {
074                            session = JCRFactoryUtil.createSession();
075    
076                            Node rootNode = getRootNode(session, companyId);
077                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
078    
079                            if (repositoryNode.hasNode(dirName)) {
080                                    throw new DuplicateDirectoryException(dirName);
081                            }
082                            else {
083                                    String[] dirNameArray = StringUtil.split(dirName, "/");
084    
085                                    Node dirNode = repositoryNode;
086    
087                                    for (int i = 0; i < dirNameArray.length; i++) {
088                                            if (Validator.isNotNull(dirNameArray[i])) {
089                                                    if (dirNode.hasNode(dirNameArray[i])) {
090                                                            dirNode = dirNode.getNode(dirNameArray[i]);
091                                                    }
092                                                    else {
093                                                            dirNode = dirNode.addNode(
094                                                                    dirNameArray[i], JCRConstants.NT_FOLDER);
095                                                    }
096                                            }
097                                    }
098    
099                                    session.save();
100                            }
101                    }
102                    catch (RepositoryException re) {
103                            throw new SystemException(re);
104                    }
105                    finally {
106                            if (session != null) {
107                                    session.logout();
108                            }
109                    }
110            }
111    
112            public void addFile(
113                            long companyId, String portletId, long groupId, long repositoryId,
114                            String fileName, long fileEntryId, String properties,
115                            Date modifiedDate, ServiceContext serviceContext, InputStream is)
116                    throws PortalException, SystemException {
117    
118                    Session session = null;
119    
120                    try {
121                            session = JCRFactoryUtil.createSession();
122    
123                            Node rootNode = getRootNode(session, companyId);
124                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
125    
126                            if (repositoryNode.hasNode(fileName)) {
127                                    throw new DuplicateFileException(fileName);
128                            }
129                            else {
130                                    Node fileNode = repositoryNode.addNode(
131                                            fileName, JCRConstants.NT_FILE);
132    
133                                    Node contentNode = fileNode.addNode(
134                                            JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
135    
136                                    contentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
137                                    contentNode.setProperty(
138                                            JCRConstants.JCR_MIME_TYPE, "text/plain");
139                                    contentNode.setProperty(JCRConstants.JCR_DATA, is);
140                                    contentNode.setProperty(
141                                            JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
142    
143                                    session.save();
144    
145                                    Version version = contentNode.checkin();
146    
147                                    contentNode.getVersionHistory().addVersionLabel(
148                                            version.getName(), DEFAULT_VERSION, false);
149    
150                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
151                                            FileModel.class);
152    
153                                    FileModel fileModel = new FileModel();
154    
155                                    fileModel.setAssetCategoryIds(
156                                            serviceContext.getAssetCategoryIds());
157                                    fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
158                                    fileModel.setCompanyId(companyId);
159                                    fileModel.setFileEntryId(fileEntryId);
160                                    fileModel.setFileName(fileName);
161                                    fileModel.setGroupId(groupId);
162                                    fileModel.setModifiedDate(modifiedDate);
163                                    fileModel.setPortletId(portletId);
164                                    fileModel.setProperties(properties);
165                                    fileModel.setRepositoryId(repositoryId);
166    
167                                    indexer.reindex(fileModel);
168                            }
169                    }
170                    catch (RepositoryException re) {
171                            throw new SystemException(re);
172                    }
173                    finally {
174                            if (session != null) {
175                                    session.logout();
176                            }
177                    }
178            }
179    
180            public void checkRoot(long companyId) throws SystemException {
181                    Session session = null;
182    
183                    try {
184                            session = JCRFactoryUtil.createSession();
185    
186                            getRootNode(session, companyId);
187    
188                            session.save();
189                    }
190                    catch (RepositoryException re) {
191                            throw new SystemException(re);
192                    }
193                    finally {
194                            if (session != null) {
195                                    session.logout();
196                            }
197                    }
198            }
199    
200            public void deleteDirectory(
201                            long companyId, String portletId, long repositoryId, String dirName)
202                    throws PortalException {
203    
204                    Session session = null;
205    
206                    try {
207                            session = JCRFactoryUtil.createSession();
208    
209                            Node rootNode = getRootNode(session, companyId);
210                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
211                            Node dirNode = repositoryNode.getNode(dirName);
212    
213                            deleteDirectory(companyId, portletId, repositoryId, dirNode);
214    
215                            dirNode.remove();
216    
217                            session.save();
218                    }
219                    catch (PathNotFoundException pnfe) {
220                            throw new NoSuchDirectoryException(dirName);
221                    }
222                    catch (RepositoryException re) {
223                            String message = GetterUtil.getString(re.getMessage());
224    
225                            if (message.contains("failed to resolve path")) {
226                                    throw new NoSuchDirectoryException(dirName);
227                            }
228                            else {
229                                    throw new PortalException(re);
230                            }
231                    }
232                    finally {
233                            if (session != null) {
234                                    session.logout();
235                            }
236                    }
237            }
238    
239            public void deleteFile(
240                            long companyId, String portletId, long repositoryId,
241                            String fileName)
242                    throws PortalException, SystemException {
243    
244                    Session session = null;
245    
246                    // A bug in Jackrabbit requires us to create a dummy node and delete the
247                    // version tree manually to successfully delete a file
248    
249                    // Create a dummy node
250    
251                    try {
252                            session = JCRFactoryUtil.createSession();
253    
254                            Node rootNode = getRootNode(session, companyId);
255                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
256                            Node fileNode = repositoryNode.getNode(fileName);
257                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
258    
259                            contentNode.checkout();
260    
261                            contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
262                            contentNode.setProperty(JCRConstants.JCR_DATA, "");
263                            contentNode.setProperty(
264                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
265    
266                            session.save();
267    
268                            Version version = contentNode.checkin();
269    
270                            contentNode.getVersionHistory().addVersionLabel(
271                                    version.getName(), "0.0", false);
272                    }
273                    catch (PathNotFoundException pnfe) {
274                            throw new NoSuchFileException(fileName);
275                    }
276                    catch (RepositoryException re) {
277                            throw new SystemException(re);
278                    }
279                    finally {
280                            if (session != null) {
281                                    session.logout();
282                            }
283                    }
284    
285                    // Delete version tree
286    
287                    try {
288                            session = JCRFactoryUtil.createSession();
289    
290                            Node rootNode = getRootNode(session, companyId);
291                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
292                            Node fileNode = repositoryNode.getNode(fileName);
293                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
294    
295                            VersionHistory versionHistory = contentNode.getVersionHistory();
296    
297                            VersionIterator itr = versionHistory.getAllVersions();
298    
299                            while (itr.hasNext()) {
300                                    Version version = itr.nextVersion();
301    
302                                    if (itr.getPosition() == itr.getSize()) {
303                                            break;
304                                    }
305                                    else {
306                                            if (!StringUtils.equals(
307                                                            JCRConstants.JCR_ROOT_VERSION, version.getName())) {
308    
309                                                    versionHistory.removeVersion(version.getName());
310                                            }
311                                    }
312                            }
313    
314                            session.save();
315                    }
316                    catch (PathNotFoundException pnfe) {
317                            throw new NoSuchFileException(fileName);
318                    }
319                    catch (RepositoryException re) {
320                            throw new SystemException(re);
321                    }
322                    finally {
323                            if (session != null) {
324                                    session.logout();
325                            }
326                    }
327    
328                    // Delete file
329    
330                    try {
331                            session = JCRFactoryUtil.createSession();
332    
333                            Node rootNode = getRootNode(session, companyId);
334                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
335                            Node fileNode = repositoryNode.getNode(fileName);
336    
337                            Indexer indexer = IndexerRegistryUtil.getIndexer(
338                                    FileModel.class);
339    
340                            FileModel fileModel = new FileModel();
341    
342                            fileModel.setCompanyId(companyId);
343                            fileModel.setFileName(fileName);
344                            fileModel.setPortletId(portletId);
345                            fileModel.setRepositoryId(repositoryId);
346    
347                            indexer.delete(fileModel);
348    
349                            fileNode.remove();
350    
351                            session.save();
352                    }
353                    catch (PathNotFoundException pnfe) {
354                            throw new NoSuchFileException(fileName);
355                    }
356                    catch (RepositoryException re) {
357                            throw new SystemException(re);
358                    }
359                    finally {
360                            if (session != null) {
361                                    session.logout();
362                            }
363                    }
364            }
365    
366            public void deleteFile(
367                            long companyId, String portletId, long repositoryId,
368                            String fileName, String versionNumber)
369                    throws PortalException, SystemException {
370    
371                    String versionLabel = versionNumber;
372    
373                    Session session = null;
374    
375                    try {
376                            session = JCRFactoryUtil.createSession();
377    
378                            Node rootNode = getRootNode(session, companyId);
379                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
380                            Node fileNode = repositoryNode.getNode(fileName);
381                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
382    
383                            VersionHistory versionHistory = contentNode.getVersionHistory();
384    
385                            Version version = versionHistory.getVersionByLabel(versionLabel);
386    
387                            versionHistory.removeVersion(version.getName());
388    
389                            session.save();
390                    }
391                    catch (PathNotFoundException pnfe) {
392                            throw new NoSuchFileException(fileName);
393                    }
394                    catch (RepositoryException re) {
395                            throw new SystemException(re);
396                    }
397                    finally {
398                            if (session != null) {
399                                    session.logout();
400                            }
401                    }
402            }
403    
404            public InputStream getFileAsStream(
405                            long companyId, long repositoryId, String fileName,
406                            String versionNumber)
407                    throws PortalException, SystemException {
408    
409                    InputStream is = null;
410    
411                    Session session = null;
412    
413                    try {
414                            session = JCRFactoryUtil.createSession();
415    
416                            Node contentNode = getFileContentNode(
417                                    session, companyId, repositoryId, fileName, versionNumber);
418    
419                            Property data = contentNode.getProperty(JCRConstants.JCR_DATA);
420    
421                            is = new UnsyncBufferedInputStream(data.getStream());
422                    }
423                    catch (RepositoryException re) {
424                            throw new SystemException(re);
425                    }
426                    finally {
427                            if (session != null) {
428                                    session.logout();
429                            }
430                    }
431    
432                    return is;
433            }
434    
435            public String[] getFileNames(
436                            long companyId, long repositoryId, String dirName)
437                    throws PortalException, SystemException {
438    
439                    List<String> fileNames = new ArrayList<String>();
440    
441                    Session session = null;
442    
443                    try {
444                            session = JCRFactoryUtil.createSession();
445    
446                            Node rootNode = getRootNode(session, companyId);
447                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
448                            Node dirNode = repositoryNode.getNode(dirName);
449    
450                            NodeIterator itr = dirNode.getNodes();
451    
452                            while (itr.hasNext()) {
453                                    Node node = (Node)itr.next();
454    
455                                    if (node.getPrimaryNodeType().getName().equals(
456                                                    JCRConstants.NT_FILE)) {
457    
458                                            fileNames.add(dirName + "/" + node.getName());
459                                    }
460                            }
461                    }
462                    catch (PathNotFoundException pnfe) {
463                            throw new NoSuchDirectoryException(dirName);
464                    }
465                    catch (RepositoryException re) {
466                            throw new SystemException(re);
467                    }
468                    finally {
469                            if (session != null) {
470                                    session.logout();
471                            }
472                    }
473    
474                    return fileNames.toArray(new String[fileNames.size()]);
475            }
476    
477            public long getFileSize(
478                            long companyId, long repositoryId, String fileName)
479                    throws PortalException, SystemException {
480    
481                    long size;
482    
483                    Session session = null;
484    
485                    try {
486                            session = JCRFactoryUtil.createSession();
487    
488                            Node contentNode = getFileContentNode(
489                                    session, companyId, repositoryId, fileName, StringPool.BLANK);
490    
491                            size = contentNode.getProperty(JCRConstants.JCR_DATA).getLength();
492                    }
493                    catch (RepositoryException re) {
494                            throw new SystemException(re);
495                    }
496                    finally {
497                            if (session != null) {
498                                    session.logout();
499                            }
500                    }
501    
502                    return size;
503            }
504    
505            public boolean hasFile(
506                            long companyId, long repositoryId, String fileName,
507                            String versionNumber)
508                    throws PortalException, SystemException {
509    
510                    try {
511                            getFileContentNode(
512                                    companyId, repositoryId, fileName, versionNumber);
513                    }
514                    catch (NoSuchFileException nsfe) {
515                            return false;
516                    }
517    
518                    return true;
519            }
520    
521            public void move(String srcDir, String destDir) throws SystemException {
522                    Session session = null;
523    
524                    try {
525                            session = JCRFactoryUtil.createSession();
526    
527                            session.move(srcDir, destDir);
528    
529                            session.save();
530                    }
531                    catch (RepositoryException re) {
532                            throw new SystemException(re);
533                    }
534                    finally {
535                            if (session != null) {
536                                    session.logout();
537                            }
538                    }
539            }
540    
541            public void reindex(String[] ids) throws SearchException {
542                    long companyId = GetterUtil.getLong(ids[0]);
543                    String portletId = ids[1];
544                    long groupId = GetterUtil.getLong(ids[2]);
545                    long repositoryId = GetterUtil.getLong(ids[3]);
546    
547                    Collection<Document> documents = new ArrayList<Document>();
548    
549                    Session session = null;
550    
551                    try {
552                            session = JCRFactoryUtil.createSession();
553    
554                            Node rootNode = getRootNode(session, companyId);
555                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
556    
557                            NodeIterator itr = repositoryNode.getNodes();
558    
559                            while (itr.hasNext()) {
560                                    Node node = (Node)itr.next();
561    
562                                    if (node.getPrimaryNodeType().getName().equals(
563                                                    JCRConstants.NT_FILE)) {
564    
565                                            Indexer indexer = IndexerRegistryUtil.getIndexer(
566                                                    FileModel.class);
567    
568                                            FileModel fileModel = new FileModel();
569    
570                                            fileModel.setCompanyId(companyId);
571                                            fileModel.setFileName(node.getName());
572                                            fileModel.setGroupId(groupId);
573                                            fileModel.setPortletId(portletId);
574                                            fileModel.setRepositoryId(repositoryId);
575    
576                                            Document document = indexer.getDocument(fileModel);
577    
578                                            if (document == null) {
579                                                    continue;
580                                            }
581    
582                                            documents.add(document);
583                                    }
584                            }
585                    }
586                    catch (Exception e1) {
587                            throw new SearchException(e1);
588                    }
589                    finally {
590                            try {
591                                    if (session != null) {
592                                            session.logout();
593                                    }
594                            }
595                            catch (Exception e) {
596                                    _log.error(e);
597                            }
598                    }
599    
600                    SearchEngineUtil.updateDocuments(companyId, documents);
601            }
602    
603            public void updateFile(
604                            long companyId, String portletId, long groupId, long repositoryId,
605                            long newRepositoryId, String fileName, long fileEntryId)
606                    throws PortalException, SystemException {
607    
608                    Session session = null;
609    
610                    try {
611                            session = JCRFactoryUtil.createSession();
612    
613                            Node rootNode = getRootNode(session, companyId);
614                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
615                            Node fileNode = repositoryNode.getNode(fileName);
616                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
617    
618                            Node newRepositoryNode = getFolderNode(rootNode, newRepositoryId);
619    
620                            if (newRepositoryNode.hasNode(fileName)) {
621                                    throw new DuplicateFileException(fileName);
622                            }
623                            else {
624                                    Node newFileNode = newRepositoryNode.addNode(
625                                            fileName, JCRConstants.NT_FILE);
626    
627                                    Node newContentNode = newFileNode.addNode(
628                                            JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
629    
630                                    VersionHistory versionHistory = contentNode.getVersionHistory();
631    
632                                    String[] versionLabels = versionHistory.getVersionLabels();
633    
634                                    for (int i = (versionLabels.length - 1); i >= 0; i--) {
635                                            Version version = versionHistory.getVersionByLabel(
636                                                    versionLabels[i]);
637    
638                                            Node frozenContentNode = version.getNode(
639                                                    JCRConstants.JCR_FROZEN_NODE);
640    
641                                            if (i == (versionLabels.length - 1)) {
642                                                    newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
643                                            }
644                                            else {
645                                                    newContentNode.checkout();
646                                            }
647    
648                                            newContentNode.setProperty(
649                                                    JCRConstants.JCR_MIME_TYPE, "text/plain");
650                                            newContentNode.setProperty(
651                                                    JCRConstants.JCR_DATA,
652                                                    frozenContentNode.getProperty(
653                                                            JCRConstants.JCR_DATA).getStream());
654                                            newContentNode.setProperty(
655                                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
656    
657                                            session.save();
658    
659                                            Version newVersion = newContentNode.checkin();
660    
661                                            newContentNode.getVersionHistory().addVersionLabel(
662                                                    newVersion.getName(), versionLabels[i],
663                                                    PropsValues.DL_HOOK_JCR_MOVE_VERSION_LABELS);
664                                    }
665    
666                                    fileNode.remove();
667    
668                                    session.save();
669    
670                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
671                                            FileModel.class);
672    
673                                    FileModel fileModel = new FileModel();
674    
675                                    fileModel.setCompanyId(companyId);
676                                    fileModel.setFileName(fileName);
677                                    fileModel.setPortletId(portletId);
678                                    fileModel.setRepositoryId(repositoryId);
679    
680                                    indexer.delete(fileModel);
681    
682                                    fileModel.setRepositoryId(newRepositoryId);
683                                    fileModel.setGroupId(groupId);
684    
685                                    indexer.reindex(fileModel);
686                            }
687                    }
688                    catch (PathNotFoundException pnfe) {
689                            throw new NoSuchFileException(fileName);
690                    }
691                    catch (RepositoryException re) {
692                            throw new SystemException(re);
693                    }
694                    finally {
695                            if (session != null) {
696                                    session.logout();
697                            }
698                    }
699            }
700    
701            public void updateFile(
702                            long companyId, String portletId, long groupId, long repositoryId,
703                            String fileName, String newFileName, boolean reindex)
704                    throws PortalException, SystemException {
705    
706                    Session session = null;
707    
708                    try {
709                            session = JCRFactoryUtil.createSession();
710    
711                            Node rootNode = getRootNode(session, companyId);
712                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
713                            Node fileNode = repositoryNode.getNode(fileName);
714                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
715    
716                            Node newFileNode = repositoryNode.addNode(
717                                    newFileName, JCRConstants.NT_FILE);
718    
719                            Node newContentNode = newFileNode.addNode(
720                                    JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);
721    
722                            VersionHistory versionHistory = contentNode.getVersionHistory();
723    
724                            String[] versionLabels = versionHistory.getVersionLabels();
725    
726                            for (int i = (versionLabels.length - 1); i >= 0; i--) {
727                                    Version version = versionHistory.getVersionByLabel(
728                                            versionLabels[i]);
729    
730                                    Node frozenContentNode = version.getNode(
731                                            JCRConstants.JCR_FROZEN_NODE);
732    
733                                    if (i == (versionLabels.length - 1)) {
734                                            newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
735                                    }
736                                    else {
737                                            newContentNode.checkout();
738                                    }
739    
740                                    newContentNode.setProperty(
741                                            JCRConstants.JCR_MIME_TYPE, "text/plain");
742                                    newContentNode.setProperty(
743                                            JCRConstants.JCR_DATA,
744                                            frozenContentNode.getProperty(
745                                                    JCRConstants.JCR_DATA).getStream());
746                                    newContentNode.setProperty(
747                                            JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
748    
749                                    session.save();
750    
751                                    Version newVersion = newContentNode.checkin();
752    
753                                    newContentNode.getVersionHistory().addVersionLabel(
754                                            newVersion.getName(), versionLabels[i],
755                                            PropsValues.DL_HOOK_JCR_MOVE_VERSION_LABELS);
756                            }
757    
758                            fileNode.remove();
759    
760                            session.save();
761    
762                            if (reindex) {
763                                    Indexer indexer = IndexerRegistryUtil.getIndexer(
764                                            FileModel.class);
765    
766                                    FileModel fileModel = new FileModel();
767    
768                                    fileModel.setCompanyId(companyId);
769                                    fileModel.setFileName(fileName);
770                                    fileModel.setPortletId(portletId);
771                                    fileModel.setRepositoryId(repositoryId);
772    
773                                    indexer.delete(fileModel);
774    
775                                    fileModel.setFileName(newFileName);
776                                    fileModel.setGroupId(groupId);
777    
778                                    indexer.reindex(fileModel);
779                            }
780                    }
781                    catch (PathNotFoundException pnfe) {
782                            throw new NoSuchFileException(fileName);
783                    }
784                    catch (RepositoryException re) {
785                            throw new SystemException(re);
786                    }
787                    finally {
788                            if (session != null) {
789                                    session.logout();
790                            }
791                    }
792            }
793    
794            public void updateFile(
795                            long companyId, String portletId, long groupId, long repositoryId,
796                            String fileName, String versionNumber, String sourceFileName,
797                            long fileEntryId, String properties, Date modifiedDate,
798                            ServiceContext serviceContext, InputStream is)
799                    throws PortalException, SystemException {
800    
801                    String versionLabel = versionNumber;
802    
803                    Session session = null;
804    
805                    try {
806                            session = JCRFactoryUtil.createSession();
807    
808                            Node rootNode = getRootNode(session, companyId);
809                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
810                            Node fileNode = repositoryNode.getNode(fileName);
811                            Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
812    
813                            contentNode.checkout();
814    
815                            contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");
816                            contentNode.setProperty(JCRConstants.JCR_DATA, is);
817                            contentNode.setProperty(
818                                    JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());
819    
820                            session.save();
821    
822                            Version version = contentNode.checkin();
823    
824                            contentNode.getVersionHistory().addVersionLabel(
825                                    version.getName(), versionLabel,
826                                    PropsValues.DL_HOOK_JCR_MOVE_VERSION_LABELS);
827    
828                            Indexer indexer = IndexerRegistryUtil.getIndexer(
829                                    FileModel.class);
830    
831                            FileModel fileModel = new FileModel();
832    
833                            fileModel.setAssetCategoryIds(serviceContext.getAssetCategoryIds());
834                            fileModel.setAssetTagNames(serviceContext.getAssetTagNames());
835                            fileModel.setCompanyId(companyId);
836                            fileModel.setFileEntryId(fileEntryId);
837                            fileModel.setFileName(fileName);
838                            fileModel.setGroupId(groupId);
839                            fileModel.setModifiedDate(modifiedDate);
840                            fileModel.setPortletId(portletId);
841                            fileModel.setProperties(properties);
842                            fileModel.setRepositoryId(repositoryId);
843    
844                            indexer.reindex(fileModel);
845                    }
846                    catch (PathNotFoundException pnfe) {
847                            throw new NoSuchFileException(fileName);
848                    }
849                    catch (RepositoryException re) {
850                            throw new SystemException(re);
851                    }
852                    finally {
853                            if (session != null) {
854                                    session.logout();
855                            }
856                    }
857            }
858    
859            protected void deleteDirectory(
860                            long companyId, String portletId, long repositoryId, Node dirNode)
861                    throws SearchException {
862    
863                    try {
864                            NodeIterator itr = dirNode.getNodes();
865    
866                            FileModel fileModel = new FileModel();
867    
868                            fileModel.setCompanyId(companyId);
869                            fileModel.setPortletId(portletId);
870                            fileModel.setRepositoryId(repositoryId);
871    
872                            Indexer indexer = IndexerRegistryUtil.getIndexer(FileModel.class);
873    
874                            while (itr.hasNext()) {
875                                    Node node = (Node)itr.next();
876    
877                                    String primaryNodeTypeName =
878                                            node.getPrimaryNodeType().getName();
879    
880                                    if (primaryNodeTypeName.equals(JCRConstants.NT_FOLDER)) {
881                                            deleteDirectory(companyId, portletId, repositoryId, node);
882                                    }
883                                    else if (primaryNodeTypeName.equals(JCRConstants.NT_FILE)) {
884                                            fileModel.setFileName(node.getName());
885    
886                                            indexer.delete(fileModel);
887                                    }
888                            }
889    
890                            fileModel.setFileName(dirNode.getName());
891    
892                            indexer.delete(fileModel);
893                    }
894                    catch (RepositoryException e) {
895                            _log.error(e);
896                    }
897            }
898    
899            protected Node getFileContentNode(
900                            long companyId, long repositoryId, String fileName,
901                            String versionNumber)
902                    throws PortalException, SystemException {
903    
904                    Node contentNode = null;
905    
906                    Session session = null;
907    
908                    try {
909                            session = JCRFactoryUtil.createSession();
910    
911                            contentNode = getFileContentNode(
912                                    session, companyId, repositoryId, fileName, versionNumber);
913                    }
914                    catch (RepositoryException re) {
915                            throw new SystemException(re);
916                    }
917                    finally {
918                            if (session != null) {
919                                    session.logout();
920                            }
921                    }
922    
923                    return contentNode;
924            }
925    
926            protected Node getFileContentNode(
927                            Session session, long companyId, long repositoryId,
928                            String fileName, String versionNumber)
929                    throws PortalException, SystemException {
930    
931                    String versionLabel = versionNumber;
932    
933                    Node contentNode = null;
934    
935                    try {
936                            Node rootNode = getRootNode(session, companyId);
937                            Node repositoryNode = getFolderNode(rootNode, repositoryId);
938                            Node fileNode = repositoryNode.getNode(fileName);
939                            contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);
940    
941                            if (Validator.isNotNull(versionNumber)) {
942                                    VersionHistory versionHistory =
943                                            contentNode.getVersionHistory();
944    
945                                    Version version = versionHistory.getVersionByLabel(
946                                            versionLabel);
947    
948                                    contentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);
949                            }
950                    }
951                    catch (PathNotFoundException pnfe) {
952                            throw new NoSuchFileException(fileName);
953                    }
954                    catch (RepositoryException re) {
955                            throw new SystemException(re);
956                    }
957    
958                    return contentNode;
959            }
960    
961            protected Node getFolderNode(Node node, long name)
962                    throws RepositoryException {
963    
964                    return getFolderNode(node, String.valueOf(name));
965            }
966    
967            protected Node getFolderNode(Node node, String name)
968                    throws RepositoryException {
969    
970                    Node folderNode = null;
971    
972                    if (node.hasNode(name)) {
973                            folderNode = node.getNode(name);
974                    }
975                    else {
976                            folderNode = node.addNode(name, JCRConstants.NT_FOLDER);
977                    }
978    
979                    return folderNode;
980            }
981    
982            protected Node getRootNode(Session session, long companyId)
983                    throws RepositoryException {
984    
985                    Node companyNode = getFolderNode(session.getRootNode(), companyId);
986    
987                    return getFolderNode(companyNode, JCRFactory.NODE_DOCUMENTLIBRARY);
988            }
989    
990            private static Log _log = LogFactoryUtil.getLog(JCRHook.class);
991    
992    }