1 package org
.argeo
.jcr
.tabular
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.InputStream
;
8 import javax
.jcr
.Binary
;
10 import javax
.jcr
.Property
;
11 import javax
.jcr
.PropertyType
;
12 import javax
.jcr
.RepositoryException
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.argeo
.ArgeoException
;
16 import org
.argeo
.jcr
.ArgeoTypes
;
17 import org
.argeo
.jcr
.JcrUtils
;
18 import org
.argeo
.util
.CsvWriter
;
19 import org
.argeo
.util
.tabular
.TabularColumn
;
20 import org
.argeo
.util
.tabular
.TabularWriter
;
22 /** Write / reference tabular content in a JCR repository. */
23 public class JcrTabularWriter
implements TabularWriter
{
24 private Node contentNode
;
25 private ByteArrayOutputStream out
;
26 private CsvWriter csvWriter
;
28 /** Creates a table node */
29 public JcrTabularWriter(Node tableNode
, List
<TabularColumn
> columns
,
30 String contentNodeType
) {
32 for (TabularColumn column
: columns
) {
33 Node columnNode
= tableNode
.addNode(column
.getName(),
34 ArgeoTypes
.ARGEO_COLUMN
);
35 columnNode
.setProperty(Property
.JCR_REQUIRED_TYPE
,
36 PropertyType
.nameFromValue(column
.getType()));
38 contentNode
= tableNode
.addNode(Property
.JCR_CONTENT
,
40 if (contentNodeType
.equals(ArgeoTypes
.ARGEO_CSV
)) {
41 contentNode
.setProperty(Property
.JCR_MIMETYPE
, "text/csv");
42 contentNode
.setProperty(Property
.JCR_ENCODING
, "UTF-8");
43 out
= new ByteArrayOutputStream();
44 csvWriter
= new CsvWriter(out
);
46 } catch (RepositoryException e
) {
47 throw new ArgeoException("Cannot create table node " + tableNode
, e
);
51 public void appendRow(List
<?
> row
) {
52 csvWriter
.writeLine(row
);
57 InputStream in
= null;
59 // TODO parallelize with pipes and writing from another thread
60 in
= new ByteArrayInputStream(out
.toByteArray());
61 binary
= contentNode
.getSession().getValueFactory()
63 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
64 } catch (RepositoryException e
) {
65 throw new ArgeoException("Cannot store data in " + contentNode
, e
);
67 IOUtils
.closeQuietly(in
);
68 JcrUtils
.closeQuietly(binary
);