1. Project Clover database Sun May 19 2019 23:30:46 BRT
  2. Package net.technearts

File Repository.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
50% of files have more coverage

Code metrics

28
55
8
1
156
140
35
0.64
6.88
8
4.38

Classes

Class Line # Actions
Repository 27 55 0% 35 19
0.791208879.1%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    package net.technearts;
2   
3    import java.util.ArrayList;
4    import java.util.Arrays;
5    import java.util.Collection;
6    import java.util.Deque;
7    import java.util.LinkedList;
8    import java.util.List;
9    import java.util.Map;
10    import java.util.Map.Entry;
11    import java.util.NavigableSet;
12    import java.util.Optional;
13    import java.util.Queue;
14    import java.util.Set;
15    import java.util.SortedSet;
16    import java.util.TreeSet;
17    import java.util.function.Predicate;
18   
19    import org.apache.commons.beanutils.PropertyUtils;
20    import org.apache.commons.lang3.StringUtils;
21    import org.slf4j.Logger;
22    import org.slf4j.LoggerFactory;
23   
24    import com.google.common.collect.HashBasedTable;
25    import com.google.common.collect.Table;
26   
 
27    public class Repository {
28    private static final Logger LOG = LoggerFactory.getLogger("Marshaller");
29    private Table<Mapping, Integer, Object> table = HashBasedTable.create();
30    private Table<Member, Integer, String> nonMappedMembers = HashBasedTable.create();
31    private List<Predicate<Object>> entityPredicates = new ArrayList<>();
32   
 
33  65 toggle public void put(Mapping mapping, int line, Object value) {
34  65 if (entityPredicates.stream().allMatch(predicate -> predicate.test(value))) {
35  65 table.put(mapping, line, value);
36    }
37    }
38   
 
39  40 toggle public Object get(String name, int line) {
40  40 if (name == null) {
41  0 throw new IllegalArgumentException("Nome não pode ser nulo");
42    }
43  40 return table.column(line).entrySet().stream()
44    .filter(entry -> name.equals(entry.getKey().getName())).findFirst().orElse(null);
45    }
46   
 
47  15 toggle public Map<Integer, Object> get(String name) {
48  15 for (Entry<Mapping, Map<Integer, Object>> entry : table.rowMap().entrySet()) {
49  45 if (name.equals(entry.getKey().getName())) {
50  15 return entry.getValue();
51    }
52    }
53  0 return null;
54    }
55   
 
56  5 toggle public Map<Integer, Object> get(Class<?> klazz) {
57  5 for (Entry<Mapping, Map<Integer, Object>> entry : table.rowMap().entrySet()) {
58  15 if (klazz.getName().equals(entry.getKey().getClassName())) {
59  5 return entry.getValue();
60    }
61    }
62  0 return null;
63    }
64   
 
65  0 toggle public void addFilter(Predicate<Object> predicate) {
66  0 entityPredicates.add(predicate);
67    }
68   
 
69  155 toggle @SuppressWarnings("unchecked")
70    public final <T, V> void set(final T bean, final Member member, final V value, String separator,
71    List<Mapping> mappings) {
72  155 final String name = member.getProperty();
73  155 final String mappedBy = member.getMappedBy();
74  155 final String converter = member.getConverter();
75  155 try {
76  155 Class<T> klass = (Class<T>) PropertyUtils.getPropertyType(bean, name);
77  155 if (Arrays.asList(klass.getInterfaces()).contains(Collection.class)) {
78  30 Collection<V> c = (Collection<V>) PropertyUtils.getProperty(bean, name);
79  30 if (c == null) {
80  30 c = (Collection<V>) newCollectionInstance(klass);
81  30 PropertyUtils.setProperty(bean, name, c);
82    }
83  30 if (value == null) {
84  10 return;
85    }
86    // embedded
87  20 if (!member.isReferenceBased()) {
88  0 c.add(value);
89  0 return;
90    }
91    // multivalued reference
92  20 for (String o : value.toString().split(separator)) {
93  30 if (StringUtils.isBlank(mappedBy)) {
94  15 c.add((V) o);
95    } else {
96  15 for (Object v : this.get(converter).values()) {
97  30 try {
98  30 String property = mappedBy;
99  30 Optional<Mapping> optionalMapping = mappings.stream()
100    .filter(mapping -> mapping.getName().equals(converter)).findFirst();
101  30 if (optionalMapping.isPresent()) {
102  30 Member m = optionalMapping.get().getMember(mappedBy);
103  30 if (m != null) {
104  0 property = m.getProperty();
105    } else {
106    // TODO ?
107    }
108    } else {
109    // TODO ?
110    }
111  30 String element = PropertyUtils.getProperty(v, property).toString().trim();
112  30 if (o.trim().equals(element)) {
113  15 c.add((V) v);
114    }
115    } catch (NullPointerException e) {
116  0 throw new IllegalArgumentException(
117    "Mapeamento " + mappedBy + " da classe " + converter + " não encontrado.");
118    }
119    }
120    }
121    }
122    } else {
123  125 PropertyUtils.setProperty(bean, name, value);
124    }
125    } catch (ReflectiveOperationException e) {
126  0 LOG.error(
127    "A propriedade " + name + " não foi encontrada em " + bean.getClass().getSimpleName());
128    }
129    }
130   
 
131  60 toggle @SuppressWarnings("unchecked")
132    private <T> T newCollectionInstance(Class<T> klass) {
133  60 T t = null;
134  60 try {
135  60 t = (T) klass.newInstance();
136    } catch (NullPointerException | InstantiationException | IllegalAccessException e) {
137  30 if (NavigableSet.class.equals(klass) || Set.class.equals(klass)
138    || SortedSet.class.equals(klass)
139    || Arrays.asList(klass.getInterfaces()).contains(NavigableSet.class)
140    || Arrays.asList(klass.getInterfaces()).contains(Set.class)
141    || Arrays.asList(klass.getInterfaces()).contains(SortedSet.class)) {
142  0 t = (T) newCollectionInstance(TreeSet.class);
143  30 } else if (List.class.equals(klass) || Queue.class.equals(klass) || Deque.class.equals(klass)
144    || Arrays.asList(klass.getInterfaces()).contains(List.class)
145    || Arrays.asList(klass.getInterfaces()).contains(Queue.class)
146    || Arrays.asList(klass.getInterfaces()).contains(Deque.class)) {
147  30 t = (T) newCollectionInstance(LinkedList.class);
148    }
149    }
150  60 return t;
151    }
152   
 
153  10 toggle public <T, V> void set(Member member, int line, V value) {
154  10 nonMappedMembers.put(member, line, value.toString());
155    }
156    }