Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.orchestration

File JpaContentHandler.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
31% of files have more coverage

Code metrics

32
138
29
1
365
327
56
0.41
4.76
29
1.93

Classes

Class Line # Actions
JpaContentHandler 23 138 56
0.5979959.8%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    package io.sunshower.service.orchestration;
2   
3    import io.reactivex.subjects.Subject;
4    import io.sunshower.common.Identifier;
5    import io.sunshower.service.git.GitRepository;
6    import io.sunshower.service.graph.service.ContentResolver;
7    import io.sunshower.service.hal.core.*;
8    import io.sunshower.service.hal.core.contents.ContentHandler;
9    import io.sunshower.service.model.Property;
10    import io.sunshower.service.model.PropertyAwareObject;
11    import io.sunshower.service.orchestration.model.Template;
12    import io.sunshower.service.orchestration.model.TemplateEvent;
13    import java.io.File;
14    import java.io.IOException;
15    import java.nio.file.Path;
16    import java.nio.file.Paths;
17    import java.util.*;
18    import java.util.stream.Collectors;
19    import java.util.stream.Stream;
20    import javax.persistence.EntityManager;
21    import org.springframework.transaction.PlatformTransactionManager;
22   
 
23    public class JpaContentHandler implements ContentHandler {
24   
25    private static final String CONTENT_PATH = "orchestrations/%s/%s/%s";
26    private static final String GRAPH_PATH = "orchestrations/%s/orchestration.hsf";
27   
28    private final Subject<TemplateEvent> topic;
29   
30    private Graph graph;
31    private Element element;
32   
33    final boolean forGraph;
34   
35    final Identifier entityId;
36    final GitRepository repository;
37    final GraphSerializationContext graphContext;
38    final Template template;
39    final EntityManager entityManager;
40    final PlatformTransactionManager transactionManager;
41   
 
42  22 toggle public JpaContentHandler(
43    boolean forGraph,
44    Identifier id,
45    Subject<TemplateEvent> topic,
46    GitRepository repository,
47    EntityManager entityManager,
48    Template template,
49    GraphSerializationContext graphContext,
50    PlatformTransactionManager transactionManager) {
51  22 if (!forGraph) {
52  20 Objects.requireNonNull(id);
53    }
54  22 this.topic = topic;
55  22 this.entityId = id;
56  22 this.forGraph = forGraph;
57  22 this.template = template;
58  22 this.repository = repository;
59  22 this.graphContext = graphContext;
60  22 this.entityManager = entityManager;
61  22 this.transactionManager = transactionManager;
62    }
63   
 
64  20 toggle public JpaContentHandler(
65    Identifier id,
66    Subject<TemplateEvent> topic,
67    GitRepository repository,
68    EntityManager entityManager,
69    Template template,
70    GraphSerializationContext graphContext,
71    PlatformTransactionManager transactionManager) {
72  20 this(false, id, topic, repository, entityManager, template, graphContext, transactionManager);
73    }
74   
 
75  1 toggle @Override
76    public ContentHandler setProperties(Collection<Property> properties) {
77  1 check(PropertyInclusion.Graph);
78  1 graph.clearProperties();
79  1 for (Property p : properties) {
80  1 graph.addProperty(p);
81    }
82  1 return this;
83    }
84   
 
85  0 toggle @Override
86    public ContentHandler setProperties(
87    String name, PropertyInclusion property, Collection<Property> properties) {
88  0 check(property);
89  0 switch (property) {
90  0 case Graph:
91  0 setProperties(properties);
92  0 break;
93  0 case Node:
94  0 setPropertiesOn(properties, (PropertyAwareObject<?>) element);
95  0 break;
96  0 case Content:
97  0 setPropertiesOn(properties, contentFor(name));
98    }
99  0 return this;
100    }
101   
 
102  0 toggle private void setPropertiesOn(Collection<Property> properties, PropertyAwareObject<?> e) {
103  0 e.clearProperties();
104  0 for (Property p : properties) {
105  0 e.addProperty(p);
106    }
107    }
108   
 
109  0 toggle @Override
110    public ContentHandler setProperties(String name, Collection<Property> properties) {
111  0 return null;
112    }
113   
 
114  1 toggle @Override
115    public Set<Property> getProperties() {
116  1 check(PropertyInclusion.Graph);
117  1 return allProperties();
118    }
119   
 
120  0 toggle @Override
121    public Set<Property> getProperties(PropertyInclusion inclusion, String name) {
122  0 check(inclusion);
123  0 switch (inclusion) {
124  0 case Graph:
125  0 return allProperties();
126  0 case Node:
127  0 return nodeProperties();
128  0 case Content:
129  0 return contentProperties(name);
130    }
131  0 throw new IllegalStateException("Nope");
132    }
133   
 
134  0 toggle private Set<Property> contentProperties(String name) {
135  0 Set<Content> contents = element.getContents();
136  0 if (contents != null) {
137  0 for (Content c : contents) {
138  0 if (Objects.equals(c.getName(), name)) {
139  0 return new LinkedHashSet<>(c.getProperties());
140    }
141    }
142    }
143  0 return Collections.emptySet();
144    }
145   
 
146  0 toggle private Set<Property> nodeProperties() {
147  0 Set<Content> contents = element.getContents();
148  0 final Map<String, Property> properties = new LinkedHashMap<>();
149  0 if (contents != null) {
150  0 for (Content c : contents) {
151  0 List<Property> cprops = c.getProperties();
152  0 for (Property cprop : cprops) {
153  0 properties.put(cprop.getName(), cprop);
154    }
155    }
156    }
157  0 PropertyAwareObject<?> pao = (PropertyAwareObject<?>) element;
158  0 for (Property p : pao.getProperties()) {
159  0 properties.remove(p.getName());
160  0 properties.put(p.getName(), p);
161    }
162  0 return new LinkedHashSet<>(properties.values());
163    }
164   
 
165  1 toggle private Set<Property> allProperties() {
166   
167  1 final Set<Property> results = new LinkedHashSet<>();
168  1 results.addAll(graph.getProperties());
169  1 results.addAll(
170    graph
171    .getVertices()
172    .stream()
173    .flatMap(
174    t ->
175    Stream.concat(
176    t.getProperties().stream(),
177    t.getContents().stream().flatMap(u -> u.getProperties().stream())))
178    .collect(Collectors.toSet()));
179  1 results.addAll(
180    graph
181    .getEdges()
182    .stream()
183    .flatMap(
184    t ->
185    Stream.concat(
186    t.getProperties().stream(),
187    t.getContents().stream().flatMap(u -> u.getProperties().stream())))
188    .collect(Collectors.toSet()));
189  1 return results;
190    }
191   
 
192  10 toggle @Override
193    public Set<Content> list() {
194  10 check();
195  10 return element.getContents();
196    }
197   
 
198  9 toggle @Override
199    public ContentResolver resolve(Content content) {
200  9 check();
201  9 if (element.getContents().stream().noneMatch(t -> t.equals(content))) {
202  1 throw new IllegalStateException(
203    String.format("Content %s does not exist in this graph", content));
204    }
205  8 return new FileBasedContentResolver(entityId, Vertex.class, template, content, topic, element);
206    }
207   
 
208  15 toggle @Override
209    public ContentHandler addContent(Content content) {
210  15 if (content == null) {
211  0 throw new IllegalArgumentException("Cannot add null content");
212    }
213  15 check();
214  15 element.addContent(content);
215  15 create(content);
216  15 return this;
217    }
218   
 
219  0 toggle @Override
220    public ContentHandler removeContent(String name) {
221  0 if (name == null) {
222  0 throw new IllegalArgumentException("Cannot remove null content");
223    }
224  0 check();
225  0 element.getContents().stream().filter(t -> name.equals(t.getName())).forEach(this::delete);
226  0 return this;
227    }
228   
 
229  5 toggle @Override
230    public ContentHandler removeContent(Content content) {
231  5 if (content == null) {
232  0 throw new IllegalArgumentException("Cannot remove null content");
233    }
234  5 check();
235  5 if (!element.getContents().removeIf(t -> t.equals(content))) {
236  0 throw new IllegalStateException(
237    String.format("Content %s does not exist in this graph", content));
238    }
239  5 delete(content);
240  4 return this;
241    }
242   
 
243  3 toggle @Override
244    @SuppressWarnings("unchecked")
245    public ContentResolver resolve(String contentName) {
246  3 check();
247  3 return new FileBasedContentResolver(
248    entityId, Vertex.class, template, contentFor(contentName), topic, element);
249    }
250   
 
251  3 toggle private Content contentFor(String contentName) {
252  3 Set<Content> contents = element.getContents();
253  3 return contents
254    .stream()
255    .filter(t -> Objects.equals(t.getName(), contentName))
256    .findFirst()
257    .get();
258    }
259   
 
260  5 toggle private void delete(Content content) {
261  5 final Path path = Paths.get(path(content));
262  5 final Path parentDir = path.getParent();
263  5 try {
264  5 if (!path.toFile().delete()) {}
265  5 io.sunshower.io.Files.delete(parentDir.toFile());
266    } catch (IOException ex) {
267  1 throw new RuntimeException(ex);
268    }
269    }
270   
 
271  15 toggle private void create(Content content) {
272  15 final Path path = Paths.get(path(content));
273  15 final Path parentDir = path.getParent();
274  15 final File parentFile = parentDir.toFile();
275   
276  15 try {
277  15 if (!(parentFile.exists() || parentFile.mkdirs())) {
278  0 throw new RuntimeException("Couldn't create file");
279    }
280  15 final File actualFile = path.toFile();
281  15 actualFile.createNewFile();
282  15 content.setFile(new io.sunshower.model.core.io.File(actualFile.getAbsolutePath()));
283    } catch (Exception ex) {
284  0 throw new RuntimeException(
285    String.format(
286    "Failed to create file: \n\t'%s'\nChild of\n\t'%s' (exists=%s).\n\t Reason: '%s'",
287    path.toFile().getAbsolutePath(),
288    parentFile.getAbsolutePath(),
289    parentFile.exists(),
290    ex.getMessage()));
291    }
292    }
293   
 
294  12 toggle @Override
295    public void close() {
296  12 flush();
297    }
298   
 
299  13 toggle @Override
300    public void flush() {
301  13 if (graph == null) {
302  0 throw new IllegalStateException("Content handler is not open!");
303    }
304  13 repository.write(path(), graphContext.open(graph));
305    }
306   
 
307  1 toggle @Override
308    public Set<Content> destroy() {
309  1 final Set<Content> contents = new HashSet<>(list());
310  1 final Set<Content> destroyed = new HashSet<>();
311  1 for (Content content : contents) {
312  2 try {
313  2 removeContent(content);
314    } catch (Exception ex) {
315  1 destroyed.add(content);
316    }
317    }
318  1 return destroyed;
319    }
320   
 
321  42 toggle private void check() {
322  42 check(PropertyInclusion.Node);
323    }
324   
 
325  44 toggle private void check(PropertyInclusion inclusion) {
326  44 load();
327  44 if (!(forGraph || inclusion == PropertyInclusion.Graph)) {
328  42 resolveElement();
329    }
330    }
331   
 
332  20 toggle private String path(Content content) {
333  20 return new File(
334    repository.getLocal(),
335    String.format(CONTENT_PATH, template.getId(), entityId, content.getName()))
336    .getAbsolutePath();
337    }
338   
 
339  35 toggle private String path() {
340  35 return String.format(GRAPH_PATH, template.getId());
341    }
342   
 
343  44 toggle private Graph load() {
344  44 if (graph == null) {
345  22 graph = graphContext.load(repository.read(path()));
346    }
347  44 return graph;
348    }
349   
 
350  42 toggle private void resolveElement() {
351  42 for (Vertex v : graph.getVertices()) {
352  44 if (v.getId().equals(entityId)) {
353  42 element = v;
354  42 return;
355    }
356    }
357  0 for (Edge e : graph.getEdges()) {
358  0 if (e.getId().equals(entityId)) {
359  0 element = e;
360  0 return;
361    }
362    }
363  0 throw new NoSuchElementException("No content found!");
364    }
365    }