]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/tabular/JcrTabularWriter.java
Add mkfolders
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / tabular / JcrTabularWriter.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.jcr.tabular;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.util.List;
22
23 import javax.jcr.Binary;
24 import javax.jcr.Node;
25 import javax.jcr.Property;
26 import javax.jcr.PropertyType;
27 import javax.jcr.RepositoryException;
28
29 import org.apache.commons.io.IOUtils;
30 import org.argeo.ArgeoException;
31 import org.argeo.jcr.ArgeoTypes;
32 import org.argeo.jcr.JcrUtils;
33 import org.argeo.util.CsvWriter;
34 import org.argeo.util.tabular.TabularColumn;
35 import org.argeo.util.tabular.TabularWriter;
36
37 /** Write / reference tabular content in a JCR repository. */
38 public class JcrTabularWriter implements TabularWriter {
39 private Node contentNode;
40 private ByteArrayOutputStream out;
41 private CsvWriter csvWriter;
42 private final List<TabularColumn> columns;
43
44 /** Creates a table node */
45 public JcrTabularWriter(Node tableNode, List<TabularColumn> columns,
46 String contentNodeType) {
47 try {
48 this.columns = columns;
49 for (TabularColumn column : columns) {
50 String normalized = JcrUtils.replaceInvalidChars(column
51 .getName());
52 Node columnNode = tableNode.addNode(normalized,
53 ArgeoTypes.ARGEO_COLUMN);
54 columnNode.setProperty(Property.JCR_TITLE, column.getName());
55 if (column.getType() != null)
56 columnNode.setProperty(Property.JCR_REQUIRED_TYPE,
57 PropertyType.nameFromValue(column.getType()));
58 else
59 columnNode.setProperty(Property.JCR_REQUIRED_TYPE,
60 PropertyType.TYPENAME_STRING);
61 }
62 contentNode = tableNode.addNode(Property.JCR_CONTENT,
63 contentNodeType);
64 if (contentNodeType.equals(ArgeoTypes.ARGEO_CSV)) {
65 contentNode.setProperty(Property.JCR_MIMETYPE, "text/csv");
66 contentNode.setProperty(Property.JCR_ENCODING, "UTF-8");
67 out = new ByteArrayOutputStream();
68 csvWriter = new CsvWriter(out);
69 }
70 } catch (RepositoryException e) {
71 throw new ArgeoException("Cannot create table node " + tableNode, e);
72 }
73 }
74
75 public void appendRow(Object[] row) {
76 csvWriter.writeLine(row);
77 }
78
79 public void close() {
80 Binary binary = null;
81 InputStream in = null;
82 try {
83 // TODO parallelize with pipes and writing from another thread
84 in = new ByteArrayInputStream(out.toByteArray());
85 binary = contentNode.getSession().getValueFactory()
86 .createBinary(in);
87 contentNode.setProperty(Property.JCR_DATA, binary);
88 } catch (RepositoryException e) {
89 throw new ArgeoException("Cannot store data in " + contentNode, e);
90 } finally {
91 IOUtils.closeQuietly(in);
92 JcrUtils.closeQuietly(binary);
93 }
94 }
95 }