1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv.handlers;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import com.orangesignal.csv.CsvReader;
24 import com.orangesignal.csv.CsvWriter;
25 import com.orangesignal.csv.annotation.CsvEntity;
26 import com.orangesignal.csv.bean.CsvEntityOperation;
27 import com.orangesignal.csv.bean.CsvEntityTemplate;
28 import com.orangesignal.csv.filters.CsvNamedValueFilter;
29 import com.orangesignal.csv.io.CsvEntityReader;
30 import com.orangesignal.csv.io.CsvEntityWriter;
31
32
33
34
35
36
37
38
39
40
41 public class CsvEntityListHandler<T> extends AbstractBeanListHandler<T, CsvEntityTemplate<T>, CsvEntityListHandler<T>> implements CsvEntityOperation<CsvEntityListHandler<T>> {
42
43
44
45
46
47
48 private boolean disableWriteHeader;
49
50
51
52
53
54
55
56 public CsvEntityListHandler(final Class<T> entityClass) {
57 super(CsvEntityTemplate.newInstance(entityClass));
58 }
59
60
61
62
63
64
65
66
67 public CsvEntityListHandler<T> disableWriteHeader(final boolean disableWriteHeader) {
68 setDisableWriteHeader(disableWriteHeader);
69 return this;
70 }
71
72
73
74
75
76
77
78 public void setDisableWriteHeader(final boolean disableWriteHeader) {
79 this.disableWriteHeader = disableWriteHeader;
80 }
81
82 @Override
83 public CsvEntityListHandler<T> filter(final CsvNamedValueFilter filter) {
84 template.filter(filter);
85 return this;
86 }
87
88 @Override
89 public List<T> load(final CsvReader reader, final boolean ignoreScalar) throws IOException {
90 @SuppressWarnings("resource")
91 final CsvEntityReader<T> r = new CsvEntityReader<T>(reader, template);
92
93
94 final List<T> results = new ArrayList<T>();
95 final boolean order = ignoreScalar || orders != null && !orders.isEmpty();
96 int offset = 0;
97
98 List<String> values;
99 while ((values = r.readValues()) != null && (order || limit <= 0 || results.size() < limit)) {
100 if (beanFilter == null && !order && offset < this.offset) {
101 offset++;
102 continue;
103 }
104 final T entity = r.toEntity(values);
105 if (beanFilter != null) {
106 if (!beanFilter.accept(entity)) {
107 continue;
108 }
109 if (!order && offset < this.offset) {
110 offset++;
111 continue;
112 }
113 }
114 results.add(entity);
115 }
116
117 if (ignoreScalar || !order) {
118 return results;
119 }
120 return processScalar(results);
121 }
122
123 @Override
124 public void save(final List<T> entities, final CsvWriter writer) throws IOException {
125 if (entities == null) {
126 throw new IllegalArgumentException("CsvEntities must not be null");
127 }
128
129 @SuppressWarnings("resource")
130 final CsvEntityWriter<T> w = new CsvEntityWriter<T>(writer, template, disableWriteHeader);
131
132
133 for (final T entity : entities) {
134 if (entity == null || entity.getClass().getAnnotation(CsvEntity.class) == null) {
135 w.write(null);
136 continue;
137 } else if (beanFilter != null && !beanFilter.accept(entity)) {
138 continue;
139 }
140
141 w.write(entity);
142 }
143 }
144
145 }