2 * Copyright (C) 2007-2012 Mathieu Baudier
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.jcr
.tabular
;
18 import java
.io
.ByteArrayInputStream
;
19 import java
.io
.ByteArrayOutputStream
;
20 import java
.io
.InputStream
;
21 import java
.util
.List
;
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
;
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
;
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
;
44 /** Creates a table node */
45 public JcrTabularWriter(Node tableNode
, List
<TabularColumn
> columns
,
46 String contentNodeType
) {
48 this.columns
= columns
;
49 for (TabularColumn column
: columns
) {
50 String normalized
= JcrUtils
.replaceInvalidChars(column
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()));
59 columnNode
.setProperty(Property
.JCR_REQUIRED_TYPE
,
60 PropertyType
.TYPENAME_STRING
);
62 contentNode
= tableNode
.addNode(Property
.JCR_CONTENT
,
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
);
70 } catch (RepositoryException e
) {
71 throw new ArgeoException("Cannot create table node " + tableNode
, e
);
75 public void appendRow(Object
[] row
) {
76 csvWriter
.writeLine(row
);
81 InputStream in
= null;
83 // TODO parallelize with pipes and writing from another thread
84 in
= new ByteArrayInputStream(out
.toByteArray());
85 binary
= contentNode
.getSession().getValueFactory()
87 contentNode
.setProperty(Property
.JCR_DATA
, binary
);
88 } catch (RepositoryException e
) {
89 throw new ArgeoException("Cannot store data in " + contentNode
, e
);
91 IOUtils
.closeQuietly(in
);
92 JcrUtils
.closeQuietly(binary
);