1+ package org .ohdsi .webapi .tool ;
2+
3+ import java .text .SimpleDateFormat ;
4+ import java .time .Instant ;
5+ import java .util .Date ;
6+ import java .util .List ;
7+ import java .util .Optional ;
8+ import java .util .stream .Collectors ;
9+ import java .util .stream .Stream ;
10+
11+ import org .apache .shiro .SecurityUtils ;
12+ import org .ohdsi .webapi .service .AbstractDaoService ;
13+ import org .ohdsi .webapi .shiro .Entities .UserEntity ;
14+ import org .ohdsi .webapi .tool .dto .ToolDTO ;
15+ import org .springframework .stereotype .Service ;
16+
17+ @ Service
18+ public class ToolServiceImpl extends AbstractDaoService implements ToolService {
19+ private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss" ;
20+
21+ private final ToolRepository toolRepository ;
22+
23+ public ToolServiceImpl (ToolRepository toolRepository ) {
24+ this .toolRepository = toolRepository ;
25+ }
26+
27+ @ Override
28+ public List <ToolDTO > getTools () {
29+ List <Tool > tools = (isAdmin () || canManageTools ()) ? toolRepository .findAll () : toolRepository .findAllByEnabled (true );
30+ return tools .stream ()
31+ .map (this ::toDTO ).collect (Collectors .toList ());
32+ }
33+
34+ @ Override
35+ public ToolDTO saveTool (ToolDTO toolDTO ) {
36+ Tool tool = saveToolFromDTO (toolDTO , getCurrentUser ());
37+ return toDTO (toolRepository .saveAndFlush (tool ));
38+ }
39+
40+ private Tool saveToolFromDTO (ToolDTO toolDTO , UserEntity currentUser ) {
41+ Tool tool = toEntity (toolDTO );
42+ if (toolDTO .getId () == null ) {
43+ tool .setCreatedBy (currentUser );
44+ }
45+ tool .setModifiedBy (currentUser );
46+ return tool ;
47+ }
48+
49+ @ Override
50+ public ToolDTO getById (Integer id ) {
51+ return toDTO (toolRepository .findOne (id ));
52+ }
53+
54+ @ Override
55+ public void delete (Integer id ) {
56+ toolRepository .delete (id );
57+ }
58+
59+ private boolean canManageTools () {
60+ return Stream .of ("tool:put" , "tool:post" , "tool:*:delete" )
61+ .allMatch (permission -> SecurityUtils .getSubject ().isPermitted (permission ));
62+ }
63+
64+ Tool toEntity (ToolDTO toolDTO ) {
65+ boolean isNewTool = toolDTO .getId () == null ;
66+ Tool tool = isNewTool ? new Tool () : toolRepository .findOne (toolDTO .getId ());
67+ Instant currentInstant = Instant .now ();
68+ if (isNewTool ) {
69+ setCreationDetails (tool , currentInstant );
70+ } else {
71+ setModificationDetails (tool , currentInstant );
72+ }
73+ updateToolFromDTO (tool , toolDTO );
74+ return tool ;
75+ }
76+
77+ private void setCreationDetails (Tool tool , Instant currentInstant ) {
78+ tool .setCreatedDate (Date .from (currentInstant ));
79+ tool .setCreatedBy (getCurrentUser ());
80+ }
81+
82+ private void setModificationDetails (Tool tool , Instant currentInstant ) {
83+ tool .setModifiedDate (Date .from (currentInstant ));
84+ tool .setModifiedBy (getCurrentUser ());
85+ }
86+
87+ private void updateToolFromDTO (Tool tool , ToolDTO toolDTO ) {
88+ Optional .ofNullable (toolDTO .getName ()).ifPresent (tool ::setName );
89+ Optional .ofNullable (toolDTO .getUrl ()).ifPresent (tool ::setUrl );
90+ Optional .ofNullable (toolDTO .getDescription ()).ifPresent (tool ::setDescription );
91+ Optional .ofNullable (toolDTO .getEnabled ()).ifPresent (tool ::setEnabled );
92+ }
93+
94+ ToolDTO toDTO (Tool tool ) {
95+ return Optional .ofNullable (tool )
96+ .map (t -> {
97+ ToolDTO toolDTO = new ToolDTO ();
98+ toolDTO .setId (t .getId ());
99+ toolDTO .setName (t .getName ());
100+ toolDTO .setUrl (t .getUrl ());
101+ toolDTO .setDescription (t .getDescription ());
102+ Optional .ofNullable (tool .getCreatedBy ())
103+ .map (UserEntity ::getId )
104+ .map (userRepository ::findOne )
105+ .map (UserEntity ::getName )
106+ .ifPresent (toolDTO ::setCreatedByName );
107+ Optional .ofNullable (tool .getModifiedBy ())
108+ .map (UserEntity ::getId )
109+ .map (userRepository ::findOne )
110+ .map (UserEntity ::getName )
111+ .ifPresent (toolDTO ::setModifiedByName );
112+ toolDTO .setCreatedDate (t .getCreatedDate () != null ? new SimpleDateFormat (DATE_TIME_FORMAT ).format (t .getCreatedDate ()) : null );
113+ toolDTO .setModifiedDate (t .getModifiedDate () != null ? new SimpleDateFormat (DATE_TIME_FORMAT ).format (t .getModifiedDate ()) : null );
114+ toolDTO .setEnabled (t .getEnabled ());
115+ return toolDTO ;
116+ })
117+ .orElse (null );
118+ }
119+
120+ }
0 commit comments