]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/tabular/JcrTabularRowIterator.java
1 package org
.argeo
.jcr
.tabular
;
3 import java
.io
.InputStream
;
4 import java
.util
.ArrayList
;
6 import java
.util
.concurrent
.ArrayBlockingQueue
;
8 import javax
.jcr
.Binary
;
10 import javax
.jcr
.NodeIterator
;
11 import javax
.jcr
.Property
;
12 import javax
.jcr
.PropertyType
;
13 import javax
.jcr
.RepositoryException
;
15 import org
.apache
.commons
.io
.IOUtils
;
16 import org
.argeo
.ArgeoException
;
17 import org
.argeo
.jcr
.ArgeoTypes
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.argeo
.util
.CsvParser
;
20 import org
.argeo
.util
.tabular
.ArrayTabularRow
;
21 import org
.argeo
.util
.tabular
.TabularColumn
;
22 import org
.argeo
.util
.tabular
.TabularRow
;
23 import org
.argeo
.util
.tabular
.TabularRowIterator
;
25 /** Iterates over the rows of a {@link ArgeoTypes#ARGEO_TABLE} node. */
26 public class JcrTabularRowIterator
implements TabularRowIterator
{
27 private Boolean hasNext
= null;
28 private Boolean parsingCompleted
= false;
30 private Long currentRowNumber
= 0l;
32 private List
<TabularColumn
> header
= new ArrayList
<TabularColumn
>();
34 /** referenced so that we can close it */
35 private Binary binary
;
36 private InputStream in
;
38 private CsvParser csvParser
;
39 private ArrayBlockingQueue
<List
<String
>> textLines
;
41 public JcrTabularRowIterator(Node tableNode
) {
43 Node contentNode
= tableNode
.getNode(Property
.JCR_CONTENT
);
44 for (NodeIterator it
= tableNode
.getNodes(); it
.hasNext();) {
45 Node node
= it
.nextNode();
46 if (node
.isNodeType(ArgeoTypes
.ARGEO_COLUMN
)) {
47 Integer type
= PropertyType
.valueFromName(node
.getProperty(
48 Property
.JCR_REQUIRED_TYPE
).getString());
49 TabularColumn tc
= new TabularColumn(node
.getName(), type
);
51 // } else if (node.getName().equals(Property.JCR_CONTENT)) {
52 // contentNode = node;
55 // should not happen since content is mandatory
56 assert contentNode
!= null;
58 if (contentNode
.isNodeType(ArgeoTypes
.ARGEO_CSV
)) {
59 textLines
= new ArrayBlockingQueue
<List
<String
>>(1000);
60 csvParser
= new CsvParser() {
61 protected void processLine(Integer lineNumber
,
62 List
<String
> header
, List
<String
> tokens
) {
64 textLines
.put(tokens
);
65 } catch (InterruptedException e
) {
66 // TODO Auto-generated catch block
69 //textLines.add(tokens);
70 if (hasNext
== null) {
72 synchronized (JcrTabularRowIterator
.this) {
73 JcrTabularRowIterator
.this.notifyAll();
78 csvParser
.setNoHeader(true);
79 binary
= contentNode
.getProperty(Property
.JCR_DATA
).getBinary();
80 in
= binary
.getStream();
81 Thread thread
= new Thread(contentNode
.getPath() + " reader") {
86 parsingCompleted
= true;
87 IOUtils
.closeQuietly(in
);
93 } catch (RepositoryException e
) {
94 throw new ArgeoException("Cannot read table " + tableNode
, e
);
98 public synchronized boolean hasNext() {
99 // we don't know if there is anything available
100 // while (hasNext == null)
103 // } catch (InterruptedException e) {
105 // // FIXME better deal with interruption
106 // Thread.currentThread().interrupt();
111 if (!textLines
.isEmpty())
114 // maybe the parsing is finished but the flag has not been set
115 while (!parsingCompleted
&& textLines
.isEmpty())
118 } catch (InterruptedException e
) {
120 // FIXME better deal with interruption
121 Thread
.currentThread().interrupt();
126 if (!textLines
.isEmpty())
129 // (parsingCompleted && textLines.isEmpty())
132 // if (!hasNext && textLines.isEmpty()) {
134 // IOUtils.closeQuietly(in);
137 // if (binary != null) {
138 // JcrUtils.closeQuietly(binary);
146 public synchronized TabularRow
next() {
148 List
<String
> tokens
= textLines
.take();
149 List
<Object
> objs
= new ArrayList
<Object
>(tokens
.size());
150 for (String token
: tokens
) {
151 // TODO convert to other formats using header
155 return new ArrayTabularRow(objs
);
156 } catch (InterruptedException e
) {
158 // FIXME better deal with interruption
163 public void remove() {
164 throw new UnsupportedOperationException();
167 public Long
getCurrentRowNumber() {
168 return currentRowNumber
;