Clover icon

sunshower-core

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

File JpaTemplateService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
36% of files have more coverage

Code metrics

14
135
23
2
327
285
59
0.44
5.87
11.5
2.57

Classes

Class Line # Actions
JpaTemplateService 44 133 58
0.4023668840.2%
JpaTemplateService.WorkspaceRef 318 2 1
1.0100%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    package io.sunshower.service.orchestration;
2   
3    import static org.hamcrest.CoreMatchers.is;
4    import static org.hamcrest.CoreMatchers.not;
5    import static org.hamcrest.Matchers.nullValue;
6    import static org.junit.Assert.assertThat;
7   
8    import io.reactivex.Observer;
9    import io.reactivex.subjects.PublishSubject;
10    import io.reactivex.subjects.Subject;
11    import io.sunshower.common.Identifier;
12    import io.sunshower.model.core.faults.SystemException;
13    import io.sunshower.persistence.core.DistributableEntity;
14    import io.sunshower.service.BaseRepository;
15    import io.sunshower.service.git.GitRepository;
16    import io.sunshower.service.git.RepositoryService;
17    import io.sunshower.service.hal.core.*;
18    import io.sunshower.service.hal.core.contents.ContentManager;
19    import io.sunshower.service.model.LineMessage;
20    import io.sunshower.service.model.Link;
21    import io.sunshower.service.model.Message;
22    import io.sunshower.service.model.Property;
23    import io.sunshower.service.orchestration.model.*;
24    import io.sunshower.service.orchestration.service.TemplateService;
25    import io.sunshower.service.revision.model.Revision;
26    import io.sunshower.service.security.AuthenticationSession;
27    import io.sunshower.service.workspace.model.Workspace;
28    import io.sunshower.service.workspace.service.WorkspaceService;
29    import java.io.IOException;
30    import java.io.InputStream;
31    import java.util.*;
32    import java.util.function.Function;
33    import java.util.stream.Collectors;
34    import javax.inject.Inject;
35    import org.junit.Test;
36    import org.springframework.security.access.prepost.PreAuthorize;
37    import org.springframework.security.core.parameters.P;
38    import org.springframework.stereotype.Service;
39    import org.springframework.transaction.PlatformTransactionManager;
40    import org.springframework.transaction.annotation.Transactional;
41   
42    @Service
43    @Transactional
 
44    public class JpaTemplateService extends BaseRepository<Identifier, Template>
45    implements TemplateService {
46   
47    public static final String SUMMARY_PATH = "orchestrations/%s/summary.sgs";
48   
49    final Subject<TemplateEvent> topic;
50   
51    @Inject private WorkspaceService workspaceService;
52   
53    @Inject private RepositoryService repositoryService;
54   
55    @Inject private GraphSerializationContext graphContext;
56   
57    @Inject private AuthenticationSession session;
58   
59    @Inject private PlatformTransactionManager transactionManager;
60   
 
61  2 toggle public JpaTemplateService() {
62  2 super(Template.class, "Template");
63  2 this.topic = PublishSubject.create();
64    }
65   
 
66  0 toggle @Test
67    public void ensureSessionIsInjected() {
68  0 assertThat(session, is(not(nullValue())));
69    }
70   
 
71  0 toggle @Override
72    public Graph getGraph(@P("id") Identifier id, Revision revision) {
73  0 final WorkspaceRef wsref = resolveWorkspace(id);
74  0 final Workspace ws = wsref.workspace;
75  0 try (GitRepository open = repositoryService.open(ws.getRepository())) {
76  0 open.checkout(revision);
77  0 final String path = String.format("orchestrations/%s/orchestration.hsf", id);
78  0 return graphContext.load(open.read(path));
79    // TODO figure out what sort of exceptions can be thrown here
80    } catch (Exception e) {
81  0 throw new SystemException(e);
82    }
83    }
84   
 
85  22 toggle @Override
86    public ContentManager contentManager(Identifier templateId) {
87  22 final WorkspaceRef wsref = resolveWorkspace(templateId);
88  22 final Workspace ws = wsref.workspace;
89  22 final Identifier id = wsref.template.getId();
90  22 try (GitRepository open = repositoryService.open(ws.getRepository())) {
91  22 open.open();
92  22 return new JpaContentManager(
93    topic, open, getEntityManager(), wsref.template, graphContext, transactionManager);
94    } catch (Exception e) {
95  0 e.printStackTrace();
96    }
97  0 return null;
98    }
99   
 
100  0 toggle @Override
101    public GraphSummary getSummary(Identifier id, Revision revision) {
102  0 return getSummary(id);
103    }
104   
 
105  1 toggle @Override
106    public GraphSummary getSummary(Identifier tid) {
107  1 final WorkspaceRef wsref = resolveWorkspace(tid);
108  1 final Workspace ws = wsref.workspace;
109  1 final Identifier id = wsref.template.getId();
110  1 try (GitRepository open = repositoryService.open(ws.getRepository())) {
111  1 open.open();
112  1 final String sid = id.toString();
113  1 return readSummary(open, sid);
114    } catch (Exception e) {
115  0 e.printStackTrace();
116    }
117  0 return null;
118    }
119   
 
120  5 toggle @Override
121    public Graph getCurrentGraph(Identifier tid) {
122  5 final WorkspaceRef wsref = resolveWorkspace(tid);
123  5 final Workspace ws = wsref.workspace;
124  5 final Identifier id = wsref.template.getId();
125  5 try (GitRepository open = repositoryService.open(ws.getRepository())) {
126  5 open.open();
127   
128  5 final String path = String.format("orchestrations/%s/orchestration.hsf", id);
129  5 if (!open.exists(path)) {
130  2 open.write(path, graphContext.open(new Graph()));
131    }
132  5 try (InputStream read = open.read(path)) {
133  5 return graphContext.load(read);
134    }
135    } catch (Exception e) {
136  0 throw new SystemException(e);
137    }
138    }
139   
 
140  17 toggle @Override
141    public void saveGraph(Identifier tid, Graph graph) {
142  17 final WorkspaceRef wsref = resolveWorkspace(tid);
143  17 final Workspace ws = wsref.workspace;
144  17 final Identifier id = wsref.template.getId();
145  17 try (GitRepository open = repositoryService.open(ws.getRepository())) {
146  17 open.open();
147  17 final String sid = id.toString();
148  17 writeSummary(open, sid, graph);
149  17 final String path = String.format("orchestrations/%s/orchestration.hsf", sid);
150  17 if (open.exists(path)) {
151  0 try (InputStream read = open.read(path)) {
152  0 final Graph existing = graphContext.load(read);
153  0 copyProperties(existing, graph);
154    }
155    }
156  17 try (InputStream gis = graphContext.open(graph)) {
157  17 open.write(path, gis);
158    }
159    } catch (Exception e) {
160  0 throw new SystemException(e);
161    }
162    }
163   
 
164  0 toggle private void copyProperties(Graph existing, Graph graph) {
165  0 final Map<Identifier, Vertex> existingVertices =
166    existing
167    .getVertices()
168    .stream()
169    .collect(Collectors.toMap(DistributableEntity::getId, Function.identity()));
170   
171  0 final Map<Identifier, Edge> existingEdges =
172    existing
173    .getEdges()
174    .stream()
175    .collect(Collectors.toMap(DistributableEntity::getId, Function.identity()));
176   
177  0 for (Edge newEdge : graph.getEdges()) {
178  0 final Edge e = existingEdges.get(newEdge.getId());
179  0 if (e != null) {
180  0 for (Content c : e.getContents()) {
181  0 newEdge.addContent(c);
182    }
183    }
184    }
185   
186  0 for (Vertex newVertex : graph.getVertices()) {
187  0 final Vertex v = existingVertices.get(newVertex.getId());
188  0 if (v != null) {
189  0 for (Content c : v.getContents()) {
190  0 newVertex.addContent(c);
191    }
192    }
193    }
194    }
195   
 
196  0 toggle @Override
197    public Revision commit(Identifier id) {
198  0 return commit(id, new LineMessage("Saving orchestration template at: " + new Date()));
199    }
200   
 
201  0 toggle @Override
202    public Revision commit(Identifier id, Message message) {
203  0 final Workspace ws = getWorkspace(id);
204  0 try (GitRepository open = repositoryService.open(ws.getRepository())) {
205  0 return open.commit(message.getMessage());
206    } catch (Exception e) {
207  0 throw new SystemException(e);
208    }
209    }
210   
 
211  49 toggle @Override
212    @PreAuthorize("hasPermission(#id, 'io.sunshower.service.orchestration.model.Template', 'READ')")
213    public Template get(Identifier id) {
214  49 return super.get(id);
215    }
216   
 
217  0 toggle @Override
218    public Set<Revision> getRevisions(Identifier id) {
219  0 return Collections.emptySet();
220    }
221   
 
222  3 toggle @Override
223    @PreAuthorize("hasPermission(#id, 'io.sunshower.service.orchestration.model.Template', 'DELETE')")
224    public Template delete(Identifier id) {
225  3 return super.delete(id);
226    }
227   
 
228  0 toggle @Override
229    public Template link(Identifier id, Template template) {
230  0 final Template source = get(id);
231  0 source.getWorkspace().addTemplate(template);
232  0 save(template);
233  0 final TemplateLink link = new TemplateLink(source, template);
234  0 getEntityManager().persist(link);
235  0 return template;
236    }
237   
 
238  0 toggle @Override
239    public Template link(Identifier id, Identifier tid) {
240  0 final Template source = get(id);
241  0 final Template target = get(tid);
242  0 final TemplateLink link = new TemplateLink(source, target);
243  0 getEntityManager().persist(link);
244  0 return source;
245    }
246   
 
247  0 toggle private Workspace getWorkspace(Identifier id) {
248  0 return get(id).getWorkspace();
249    }
250   
 
251  1 toggle private GraphSummary readSummary(GitRepository open, String sid) throws IOException {
252  1 final String path = String.format(SUMMARY_PATH, sid);
253  1 try (InputStream read = open.read(path)) {
254  1 return graphContext.loadSummary(read);
255    }
256    }
257   
 
258  17 toggle private void writeSummary(GitRepository open, String sid, Graph graph) {
259  17 final String path = String.format(SUMMARY_PATH, sid);
260  17 open.write(path, graphContext.openSummary(graph));
261    }
262   
 
263  45 toggle private WorkspaceRef resolveWorkspace(Identifier templateId) {
264   
265  45 Template template = get(templateId);
266  45 Link<Template, Template> link = template.getLink();
267  45 while (link != null) {
268  0 template = link.getSource();
269  0 link = template.getLink();
270    }
271  45 return new WorkspaceRef(template.getWorkspace(), template);
272    }
273   
 
274  0 toggle @Override
275    public void subscribe(Observer<? super TemplateEvent> observer) {
276  0 topic.subscribe(observer);
277    }
278   
 
279  0 toggle @Override
280    @SuppressWarnings("unchecked")
281    public List<Property> getProperties(Identifier tid) {
282  0 final WorkspaceRef wsref = resolveWorkspace(tid);
283  0 final Workspace ws = wsref.workspace;
284  0 final Identifier id = wsref.template.getId();
285  0 try (GitRepository open = repositoryService.open(ws.getRepository())) {
286  0 open.open();
287  0 final String sid = id.toString();
288  0 final Set<Property> result = new LinkedHashSet<>();
289  0 final String path = String.format("orchestrations/%s/orchestration.hsf", sid);
290   
291  0 final Graph graph = graphContext.load(open.read(path));
292  0 for (Vertex v : graph.getVertices()) {
293  0 result.addAll(v.getProperties());
294  0 Set<Content> contents = v.getContents();
295  0 if (contents != null) {
296  0 for (Content c : contents) {
297  0 result.addAll(c.getProperties());
298    }
299    }
300    }
301   
302  0 for (Edge v : graph.getEdges()) {
303  0 result.addAll(v.getProperties());
304  0 Set<Content> contents = v.getContents();
305  0 if (contents != null) {
306  0 for (Content c : contents) {
307  0 result.addAll(c.getProperties());
308    }
309    }
310    }
311  0 result.addAll(graph.getProperties());
312  0 return new ArrayList<>(result);
313    } catch (Exception e) {
314  0 throw new SystemException(e);
315    }
316    }
317   
 
318    static class WorkspaceRef {
319    final Workspace workspace;
320    final Template template;
321   
 
322  45 toggle WorkspaceRef(Workspace workspace, Template template) {
323  45 this.workspace = workspace;
324  45 this.template = template;
325    }
326    }
327    }