001    /**
002     * Copyright (c) 2000-present 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.portal.repository.capabilities;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.repository.capabilities.BulkOperationCapability;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.repository.model.RepositoryModelOperation;
025    import com.liferay.portal.repository.liferayrepository.model.LiferayFileEntry;
026    import com.liferay.portal.repository.liferayrepository.model.LiferayFolder;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFolder;
029    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
030    import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
031    
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    /**
036     * @author Adolfo P??rez
037     */
038    public class LiferayBulkOperationCapability implements BulkOperationCapability {
039    
040            public LiferayBulkOperationCapability(long repositoryId) {
041                    _repositoryId = repositoryId;
042            }
043    
044            @Override
045            public void execute(
046                            BulkOperationCapability.Filter<?> filter,
047                            RepositoryModelOperation repositoryModelOperation)
048                    throws PortalException {
049    
050                    executeOnAllFileEntries(filter, repositoryModelOperation);
051                    executeOnAllFolders(filter, repositoryModelOperation);
052            }
053    
054            @Override
055            public void execute(RepositoryModelOperation repositoryModelOperation)
056                    throws PortalException {
057    
058                    execute(null, repositoryModelOperation);
059            }
060    
061            protected void executeOnAllFileEntries(
062                            Filter<?> filter, RepositoryModelOperation repositoryModelOperation)
063                    throws PortalException {
064    
065                    ActionableDynamicQuery actionableDynamicQuery =
066                            DLFileEntryLocalServiceUtil.getActionableDynamicQuery();
067    
068                    actionableDynamicQuery.setAddCriteriaMethod(
069                            new RepositoryModelAddCriteriaMethod(filter));
070                    actionableDynamicQuery.setPerformActionMethod(
071                            new FileEntryPerformActionMethod(repositoryModelOperation));
072    
073                    actionableDynamicQuery.performActions();
074            }
075    
076            protected void executeOnAllFolders(
077                            Filter<?> filter, RepositoryModelOperation repositoryModelOperation)
078                    throws PortalException {
079    
080                    ActionableDynamicQuery actionableDynamicQuery =
081                            DLFolderLocalServiceUtil.getActionableDynamicQuery();
082    
083                    actionableDynamicQuery.setAddCriteriaMethod(
084                            new RepositoryModelAddCriteriaMethod(filter));
085                    actionableDynamicQuery.setPerformActionMethod(
086                            new FolderPerformActionMethod(repositoryModelOperation));
087    
088                    actionableDynamicQuery.performActions();
089            }
090    
091            private static final Map<Class<? extends Field<?>>, String> _fieldNames =
092                    new HashMap<>();
093    
094            static {
095                    _fieldNames.put(Field.CreateDate.class, "createDate");
096            }
097    
098            private final long _repositoryId;
099    
100            private static class FileEntryPerformActionMethod
101                    implements ActionableDynamicQuery.PerformActionMethod {
102    
103                    public FileEntryPerformActionMethod(
104                            RepositoryModelOperation repositoryModelOperation) {
105    
106                            _repositoryModelOperation = repositoryModelOperation;
107                    }
108    
109                    @Override
110                    public void performAction(Object object) throws PortalException {
111                            DLFileEntry dlFileEntry = (DLFileEntry)object;
112    
113                            FileEntry fileEntry = new LiferayFileEntry(dlFileEntry);
114    
115                            fileEntry.execute(_repositoryModelOperation);
116                    }
117    
118                    private RepositoryModelOperation _repositoryModelOperation;
119    
120            }
121    
122            private static class FolderPerformActionMethod
123                    implements ActionableDynamicQuery.PerformActionMethod {
124    
125                    public FolderPerformActionMethod(
126                            RepositoryModelOperation repositoryModelOperation) {
127    
128                            _repositoryModelOperation = repositoryModelOperation;
129                    }
130    
131                    @Override
132                    public void performAction(Object object) throws PortalException {
133                            DLFolder dlFolder = (DLFolder)object;
134    
135                            if (dlFolder.isMountPoint()) {
136                                    return;
137                            }
138    
139                            Folder folder = new LiferayFolder(dlFolder);
140    
141                            folder.execute(_repositoryModelOperation);
142                    }
143    
144                    private final RepositoryModelOperation _repositoryModelOperation;
145    
146            }
147    
148            private class RepositoryModelAddCriteriaMethod
149                    implements ActionableDynamicQuery.AddCriteriaMethod {
150    
151                    public RepositoryModelAddCriteriaMethod(Filter<?> filter) {
152                            _filter = filter;
153                    }
154    
155                    @Override
156                    public void addCriteria(DynamicQuery dynamicQuery) {
157                            dynamicQuery.add(
158                                    RestrictionsFactoryUtil.eq("repositoryId", _repositoryId));
159    
160                            if (_filter != null) {
161                                    addFilterCriteria(dynamicQuery);
162                            }
163                    }
164    
165                    protected void addFilterCriteria(DynamicQuery dynamicQuery) {
166                            Class<? extends Field<?>> field = _filter.getField();
167    
168                            String fieldName = _fieldNames.get(field);
169    
170                            if (fieldName == null) {
171                                    throw new UnsupportedOperationException(
172                                            "Unsupported field " + field.getName());
173                            }
174    
175                            Operator operator = _filter.getOperator();
176    
177                            Object value = _filter.getValue();
178    
179                            if (operator == Operator.EQ) {
180                                    dynamicQuery.add(RestrictionsFactoryUtil.eq(fieldName, value));
181                            }
182                            else if (operator == Operator.LE) {
183                                    dynamicQuery.add(RestrictionsFactoryUtil.le(fieldName, value));
184                            }
185                            else if (operator == Operator.LT) {
186                                    dynamicQuery.add(RestrictionsFactoryUtil.lt(fieldName, value));
187                            }
188                            else if (operator == Operator.GE) {
189                                    dynamicQuery.add(RestrictionsFactoryUtil.ge(fieldName, value));
190                            }
191                            else if (operator == Operator.GT) {
192                                    dynamicQuery.add(RestrictionsFactoryUtil.gt(fieldName, value));
193                            }
194                    }
195    
196                    private final Filter<?> _filter;
197    
198            }
199    
200    }