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.proxy;
016    
017    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.repository.LocalRepository;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.FileShortcut;
021    import com.liferay.portal.kernel.repository.model.FileVersion;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.repository.model.RepositoryEntry;
024    import com.liferay.portal.kernel.util.ListUtil;
025    import com.liferay.portal.kernel.util.ProxyUtil;
026    
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.List;
030    
031    /**
032     * @author Mika Koivisto
033     */
034    public abstract class RepositoryModelProxyBean {
035    
036            public RepositoryModelProxyBean(ClassLoader classLoader) {
037                    _classLoader = classLoader;
038            }
039    
040            protected FileEntryProxyBean newFileEntryProxyBean(FileEntry fileEntry) {
041                    if (fileEntry == null) {
042                            return null;
043                    }
044    
045                    FileEntry fileEntryProxy = (FileEntry)newProxyInstance(
046                            fileEntry, FileEntry.class);
047    
048                    return new FileEntryProxyBean(fileEntryProxy, _classLoader);
049            }
050    
051            protected FileShortcutProxyBean newFileShortcutProxyBean(
052                    FileShortcut fileShortcut) {
053    
054                    if (fileShortcut == null) {
055                            return null;
056                    }
057    
058                    FileShortcut fileShortcutProxy = (FileShortcut)newProxyInstance(
059                            fileShortcut, FileShortcut.class);
060    
061                    return new FileShortcutProxyBean(fileShortcutProxy, _classLoader);
062            }
063    
064            protected FileVersionProxyBean newFileVersionProxyBean(
065                    FileVersion fileVersion) {
066    
067                    if (fileVersion == null) {
068                            return null;
069                    }
070    
071                    FileVersion fileVersionProxy = (FileVersion)newProxyInstance(
072                            fileVersion, FileVersion.class);
073    
074                    return new FileVersionProxyBean(fileVersionProxy, _classLoader);
075            }
076    
077            protected FolderProxyBean newFolderProxyBean(Folder folder) {
078                    if (folder == null) {
079                            return null;
080                    }
081    
082                    Folder folderProxy = (Folder)newProxyInstance(folder, Folder.class);
083    
084                    return new FolderProxyBean(folderProxy, _classLoader);
085            }
086    
087            protected LocalRepositoryProxyBean newLocalRepositoryProxyBean(
088                    LocalRepository localRepository) {
089    
090                    LocalRepository localRepositoryProxy =
091                            (LocalRepository)newProxyInstance(
092                                    localRepository, LocalRepository.class);
093    
094                    return new LocalRepositoryProxyBean(localRepositoryProxy, _classLoader);
095            }
096    
097            protected Object newProxyBean(Object bean) {
098                    if (bean instanceof FileEntry) {
099                            return newFileEntryProxyBean((FileEntry)bean);
100                    }
101                    else if (bean instanceof FileVersion) {
102                            return newFileVersionProxyBean((FileVersion)bean);
103                    }
104                    else if (bean instanceof Folder) {
105                            return newFolderProxyBean((Folder)bean);
106                    }
107                    else {
108                            return bean;
109                    }
110            }
111    
112            protected Object newProxyInstance(Object bean, Class<?> clazz) {
113                    if (bean == null) {
114                            return null;
115                    }
116    
117                    return ProxyUtil.newProxyInstance(
118                            _classLoader, new Class[] {clazz},
119                            new ClassLoaderBeanHandler(bean, _classLoader));
120            }
121    
122            protected List<FileEntry> toFileEntryProxyBeans(
123                    List<FileEntry> fileEntries) {
124    
125                    if ((fileEntries == null) || fileEntries.isEmpty()) {
126                            return fileEntries;
127                    }
128    
129                    List<FileEntry> fileEntryProxyBeans = new ArrayList<>(
130                            fileEntries.size());
131    
132                    for (FileEntry fileEntry : fileEntries) {
133                            fileEntryProxyBeans.add(newFileEntryProxyBean(fileEntry));
134                    }
135    
136                    if (ListUtil.isUnmodifiableList(fileEntries)) {
137                            return Collections.unmodifiableList(fileEntryProxyBeans);
138                    }
139    
140                    return fileEntryProxyBeans;
141            }
142    
143            protected List<FileVersion> toFileVersionProxyBeans(
144                    List<FileVersion> fileVersions) {
145    
146                    if ((fileVersions == null) || fileVersions.isEmpty()) {
147                            return fileVersions;
148                    }
149    
150                    List<FileVersion> fileVersionProxyBeans = new ArrayList<>(
151                            fileVersions.size());
152    
153                    for (FileVersion fileVersion : fileVersions) {
154                            fileVersionProxyBeans.add(newFileVersionProxyBean(fileVersion));
155                    }
156    
157                    if (ListUtil.isUnmodifiableList(fileVersions)) {
158                            return Collections.unmodifiableList(fileVersionProxyBeans);
159                    }
160    
161                    return fileVersionProxyBeans;
162            }
163    
164            protected List<Folder> toFolderProxyBeans(List<Folder> folders) {
165                    if ((folders == null) || folders.isEmpty()) {
166                            return folders;
167                    }
168    
169                    List<Folder> folderProxyBeans = new ArrayList<>(folders.size());
170    
171                    for (Folder folder : folders) {
172                            folderProxyBeans.add(newFolderProxyBean(folder));
173                    }
174    
175                    if (ListUtil.isUnmodifiableList(folders)) {
176                            return Collections.unmodifiableList(folderProxyBeans);
177                    }
178    
179                    return folderProxyBeans;
180            }
181    
182            protected List<RepositoryEntry> toObjectProxyBeans(
183                    List<RepositoryEntry> repositoryEntries) {
184    
185                    if ((repositoryEntries == null) || repositoryEntries.isEmpty()) {
186                            return repositoryEntries;
187                    }
188    
189                    List<RepositoryEntry> objectProxyBeans = new ArrayList<>();
190    
191                    for (RepositoryEntry repositoryEntry : repositoryEntries) {
192                            objectProxyBeans.add(
193                                    (RepositoryEntry)newProxyBean(repositoryEntry));
194                    }
195    
196                    if (ListUtil.isUnmodifiableList(repositoryEntries)) {
197                            return Collections.unmodifiableList(objectProxyBeans);
198                    }
199    
200                    return objectProxyBeans;
201            }
202    
203            private final ClassLoader _classLoader;
204    
205    }