View Javadoc

1   package com.eyeq.pivot4j.pentaho.repository;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.util.ArrayList;
8   import java.util.LinkedList;
9   import java.util.List;
10  
11  import javax.annotation.PostConstruct;
12  
13  import org.apache.commons.configuration.ConfigurationException;
14  import org.apache.commons.lang.NullArgumentException;
15  import org.pentaho.platform.api.engine.IPentahoSession;
16  import org.pentaho.platform.api.repository2.unified.IRepositoryFileData;
17  import org.pentaho.platform.api.repository2.unified.IUnifiedRepository;
18  import org.pentaho.platform.api.repository2.unified.RepositoryFile;
19  import org.pentaho.platform.api.repository2.unified.data.simple.SimpleRepositoryFileData;
20  import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
21  import org.pentaho.platform.engine.core.system.PentahoSystem;
22  import org.pentaho.platform.repository2.unified.RepositoryUtils;
23  
24  import com.eyeq.pivot4j.analytics.repository.AbstractFileSystemRepository;
25  import com.eyeq.pivot4j.analytics.repository.ReportContent;
26  import com.eyeq.pivot4j.analytics.repository.ReportFile;
27  
28  import edu.emory.mathcs.backport.java.util.Arrays;
29  
30  public class PentahoReportRepository extends AbstractFileSystemRepository {
31  
32  	private IPentahoSession session;
33  
34  	private IUnifiedRepository repository;
35  
36  	@PostConstruct
37  	protected void initialize() {
38  		this.session = PentahoSessionHolder.getSession();
39  		this.repository = PentahoSystem.get(IUnifiedRepository.class, session);
40  	}
41  
42  	/**
43  	 * @return the session
44  	 */
45  	protected IPentahoSession getSession() {
46  		return session;
47  	}
48  
49  	/**
50  	 * @return the repository
51  	 */
52  	protected IUnifiedRepository getRepository() {
53  		return repository;
54  	}
55  
56  	/**
57  	 * @throws IOException
58  	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#getRoot()
59  	 */
60  	@Override
61  	public PentahoReportFile getRoot() throws IOException {
62  		return getFile(ReportFile.SEPARATOR);
63  	}
64  
65  	/**
66  	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#exists(java.lang.String)
67  	 */
68  	@Override
69  	public boolean exists(String path) throws IOException {
70  		return getFile(path) != null;
71  	}
72  
73  	/**
74  	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#getFile(java.lang.String)
75  	 */
76  	@Override
77  	public PentahoReportFile getFile(String path) throws IOException {
78  		if (path == null) {
79  			throw new NullArgumentException("path");
80  		}
81  
82  		List<?> segments = Arrays.asList(path.split(ReportFile.SEPARATOR));
83  
84  		List<PentahoReportFile> ancestors = new ArrayList<PentahoReportFile>(
85  				segments.size());
86  
87  		ancestors.add(new PentahoReportFile(repository
88  				.getFile(ReportFile.SEPARATOR), null));
89  
90  		StringBuilder builder = new StringBuilder();
91  
92  		for (Object segment : segments) {
93  			String name = segment.toString();
94  
95  			if (!name.isEmpty()) {
96  				builder.append(name);
97  
98  				RepositoryFile file = repository.getFile(builder.toString());
99  
100 				if (file == null) {
101 					return null;
102 				}
103 
104 				ancestors.add(new PentahoReportFile(file, ancestors
105 						.get(ancestors.size() - 1)));
106 			}
107 
108 			builder.append(ReportFile.SEPARATOR);
109 		}
110 
111 		return ancestors.get(ancestors.size() - 1);
112 	}
113 
114 	/**
115 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#getFiles(com.eyeq.pivot4j.analytics.repository.ReportFile)
116 	 */
117 	@Override
118 	public List<ReportFile> getFiles(ReportFile parent) throws IOException {
119 		if (parent == null) {
120 			throw new NullArgumentException("parent");
121 		}
122 
123 		List<ReportFile> files = new LinkedList<ReportFile>();
124 
125 		if (parent.isDirectory()) {
126 			List<RepositoryFile> children = repository.getChildren(parent
127 					.getId());
128 
129 			for (RepositoryFile file : children) {
130 				files.add(new PentahoReportFile(file,
131 						(PentahoReportFile) parent));
132 			}
133 		}
134 
135 		return files;
136 	}
137 
138 	/**
139 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#createFile(com.eyeq.pivot4j.analytics.repository.ReportFile,
140 	 *      java.lang.String,
141 	 *      com.eyeq.pivot4j.analytics.repository.ReportContent)
142 	 */
143 	@Override
144 	public ReportFile createFile(ReportFile parent, String name,
145 			ReportContent content) throws IOException, ConfigurationException {
146 		return createFile(parent, name, content, true);
147 	}
148 
149 	/**
150 	 * @param parent
151 	 * @param name
152 	 * @param content
153 	 * @param overwrite
154 	 * @return
155 	 * @throws IOException
156 	 * @throws ConfigurationException
157 	 */
158 	public ReportFile createFile(ReportFile parent, String name,
159 			ReportContent content, boolean overwrite) throws IOException,
160 			ConfigurationException {
161 		if (parent == null) {
162 			throw new NullArgumentException("parent");
163 		}
164 
165 		if (name == null) {
166 			throw new NullArgumentException("name");
167 		}
168 
169 		if (content == null) {
170 			throw new NullArgumentException("content");
171 		}
172 
173 		RepositoryUtils utils = new RepositoryUtils(repository);
174 
175 		RepositoryFile file = utils.saveFile(parent.getPath()
176 				+ ReportFile.SEPARATOR + name, createReportData(content), true,
177 				overwrite, false, false, null);
178 
179 		return new PentahoReportFile(file, (PentahoReportFile) parent);
180 	}
181 
182 	/**
183 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#createDirectory(com.eyeq.pivot4j.analytics.repository.ReportFile,
184 	 *      java.lang.String)
185 	 */
186 	@Override
187 	public ReportFile createDirectory(ReportFile parent, String name)
188 			throws IOException {
189 		if (parent == null) {
190 			throw new NullArgumentException("parent");
191 		}
192 
193 		if (name == null) {
194 			throw new NullArgumentException("name");
195 		}
196 
197 		RepositoryFile directory = new RepositoryFile.Builder(name)
198 				.folder(true).build();
199 		directory = repository.createFolder(parent.getId(), directory, null);
200 
201 		return new PentahoReportFile(directory, (PentahoReportFile) parent);
202 	}
203 
204 	/**
205 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#renameFile(com.eyeq.pivot4j.analytics.repository.ReportFile,
206 	 *      java.lang.String)
207 	 */
208 	@Override
209 	public ReportFile renameFile(ReportFile file, String newName)
210 			throws IOException {
211 		if (file == null) {
212 			throw new NullArgumentException("file");
213 		}
214 
215 		if (newName == null) {
216 			throw new NullArgumentException("newName");
217 		}
218 
219 		String path = file.getParent().getPath() + ReportFile.SEPARATOR
220 				+ newName;
221 
222 		repository.moveFile(file.getId(), path, null);
223 
224 		return file;
225 	}
226 
227 	/**
228 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#deleteFile(com.eyeq.pivot4j.analytics.repository.ReportFile)
229 	 */
230 	@Override
231 	public void deleteFile(ReportFile file) throws IOException {
232 		if (file == null) {
233 			throw new NullArgumentException("file");
234 		}
235 
236 		repository.deleteFile(file.getId(), null);
237 	}
238 
239 	/**
240 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#readContent(com.eyeq.pivot4j.analytics.repository.ReportFile)
241 	 */
242 	@Override
243 	public InputStream readContent(ReportFile file) throws IOException {
244 		if (file == null) {
245 			throw new NullArgumentException("file");
246 		}
247 
248 		SimpleRepositoryFileData data = repository.getDataForRead(file.getId(),
249 				SimpleRepositoryFileData.class);
250 
251 		return data.getInputStream();
252 	}
253 
254 	/**
255 	 * @see com.eyeq.pivot4j.analytics.repository.ReportRepository#setReportContent(com.eyeq.pivot4j.analytics.repository.ReportFile,
256 	 *      com.eyeq.pivot4j.analytics.repository.ReportContent)
257 	 */
258 	@Override
259 	public void setReportContent(ReportFile file, ReportContent content)
260 			throws IOException, ConfigurationException {
261 		if (file == null) {
262 			throw new NullArgumentException("file");
263 		}
264 
265 		if (content == null) {
266 			throw new NullArgumentException("content");
267 		}
268 
269 		PentahoReportFile pentahoFile = (PentahoReportFile) file;
270 
271 		ByteArrayOutputStream out = new ByteArrayOutputStream();
272 		content.write(out);
273 
274 		out.flush();
275 		out.close();
276 
277 		repository.updateFile(pentahoFile.getFile(), createReportData(content),
278 				null);
279 	}
280 
281 	/**
282 	 * @param content
283 	 * @return
284 	 * @throws ConfigurationException
285 	 * @throws IOException
286 	 */
287 	protected IRepositoryFileData createReportData(ReportContent content)
288 			throws ConfigurationException, IOException {
289 		ByteArrayOutputStream out = new ByteArrayOutputStream();
290 		content.write(out);
291 
292 		out.flush();
293 		out.close();
294 
295 		return new SimpleRepositoryFileData(new ByteArrayInputStream(
296 				out.toByteArray()), "UTF-8", "text/xml");
297 	}
298 }