Clover icon

sunshower-core

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

File JpaWorkspaceService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
34
9
1
134
111
11
0.32
3.78
9
1.22

Classes

Class Line # Actions
JpaWorkspaceService 30 34 11
0.957446895.7%
 

Contributing tests

This file is covered by 61 tests. .

Source view

1    package io.sunshower.service.workspace;
2   
3    import static org.springframework.security.acls.domain.BasePermission.*;
4   
5    import io.sunshower.common.Identifier;
6    import io.sunshower.model.core.auth.User;
7    import io.sunshower.service.BaseRepository;
8    import io.sunshower.service.git.RepositoryService;
9    import io.sunshower.service.model.io.FileResolutionStrategy;
10    import io.sunshower.service.orchestration.model.Template;
11    import io.sunshower.service.revision.model.Local;
12    import io.sunshower.service.revision.model.Repositories;
13    import io.sunshower.service.revision.model.Repository;
14    import io.sunshower.service.workspace.model.Workspace;
15    import io.sunshower.service.workspace.service.WorkspaceService;
16    import java.io.File;
17    import java.nio.file.Paths;
18    import java.util.LinkedHashSet;
19    import java.util.Set;
20    import javax.inject.Inject;
21    import javax.persistence.EntityManager;
22    import javax.persistence.EntityNotFoundException;
23    import org.springframework.security.access.prepost.PreAuthorize;
24    import org.springframework.stereotype.Service;
25    import org.springframework.transaction.annotation.Transactional;
26   
27    /** Created by haswell on 5/9/17. */
28    @Service
29    @Transactional
 
30    public class JpaWorkspaceService extends BaseRepository<Identifier, Workspace>
31    implements WorkspaceService {
32   
33    @Inject private RepositoryService repositoryService;
34   
35    @Inject private FileResolutionStrategy repositoryResolutionStrategy;
36   
 
37  2 toggle public JpaWorkspaceService() {
38  2 super(Workspace.class, "Workspace");
39    }
40   
 
41  94 toggle @Override
42    @PreAuthorize("hasAuthority('tenant:user')")
43    public Workspace create(Workspace entity) {
44  94 repositoryFor(entity);
45  94 return super.create(entity);
46    }
47   
 
48  22 toggle @Override
49    @PreAuthorize("hasPermission(#id, 'io.sunshower.service.workspace.model.Workspace', 'READ')")
50    public Workspace get(Identifier id) {
51  22 return super.get(id);
52    }
53   
 
54  9 toggle @Override
55    @PreAuthorize("hasPermission(#id, 'io.sunshower.service.workspace.model.Workspace', 'DELETE')")
56    public Workspace delete(Identifier id) {
57  9 final Workspace workspace = super.delete(id);
58  9 Repositories.delete(workspace.getRepository());
59  9 return workspace;
60    }
61   
 
62  14 toggle @Override
63    public Identifier addTemplate(Identifier workspaceId, Template template) {
64  14 final Workspace ws = get(workspaceId);
65  14 if (ws == null) {
66  0 throw new EntityNotFoundException(
67    String.format("No workspace identified by '%s' was found", workspaceId));
68    }
69  14 grant(Template.class, template, ADMINISTRATION, WRITE, READ, DELETE);
70   
71  14 ws.addTemplate(template);
72  14 flush();
73  14 return template.getId();
74    }
75   
 
76  12 toggle @Override
77    public Set<Template> getTemplates(Identifier workspaceId) {
78  12 return new LinkedHashSet<>(
79    getEntityManager()
80    .createQuery(
81    "select a from Workspace w "
82    + "join w.templates a "
83    + "join w.identity woid "
84    + "join a.identity aoid "
85    + "where woid.owner.username = :id "
86    + "and aoid.owner.username = :id "
87    + "and w.id = :wsid",
88    Template.class)
89    .setParameter("id", getSession().getUsername())
90    .setParameter("wsid", workspaceId.value())
91    .getResultList());
92    }
93   
 
94  4 toggle @Override
95    public Template deleteTemplate(Identifier templateId) {
96   
97  4 EntityManager entityManager = getEntityManager();
98  4 Template orchestrationTemplate = entityManager.find(Template.class, templateId);
99  4 entityManager.remove(orchestrationTemplate);
100  4 revokeAll(Template.class, orchestrationTemplate);
101   
102  4 return orchestrationTemplate;
103    }
104   
 
105  94 toggle private Repository repositoryFor(Workspace entity) {
106  94 Repository repository = entity.getRepository();
107  94 if (repository == null) {
108  90 repository = createRepository(entity);
109  90 entity.setRepository(repository);
110    }
111  94 return repository;
112    }
113   
 
114  90 toggle private Repository createRepository(Workspace entity) {
115   
116  90 final User user = getEntityManager().find(User.class, getSession().getId());
117  90 File resolve = repositoryResolutionStrategy.resolve(user.getTenant(), user, null);
118   
119  90 final Local local = new Local();
120   
121  90 final io.sunshower.model.core.io.File file =
122    new io.sunshower.model.core.io.File(
123    Paths.get(resolve.getAbsolutePath())
124    .resolve(String.format("%s/local", entity.getId()))
125    .toString());
126   
127  90 local.setFile(file);
128   
129  90 final Repository repository = new Repository();
130  90 repository.setLocal(local);
131  90 repositoryService.open(repository).initialize();
132  90 return repository;
133    }
134    }