View Javadoc
1   package de.dlr.shepard.labJournal.services;
2   
3   import de.dlr.shepard.labJournal.dao.LabJournalEntryDAO;
4   import de.dlr.shepard.labJournal.entities.LabJournalEntry;
5   import de.dlr.shepard.neo4Core.entities.DataObject;
6   import de.dlr.shepard.neo4Core.entities.User;
7   import de.dlr.shepard.neo4Core.services.DataObjectService;
8   import de.dlr.shepard.neo4Core.services.UserService;
9   import de.dlr.shepard.util.DateHelper;
10  import jakarta.enterprise.context.RequestScoped;
11  import jakarta.inject.Inject;
12  import java.util.ArrayList;
13  import java.util.Comparator;
14  import java.util.List;
15  import java.util.stream.Collectors;
16  
17  @RequestScoped
18  public class LabJournalEntryService {
19  
20    private LabJournalEntryDAO labJournalEntryDAO;
21  
22    private DataObjectService dataObjectService;
23  
24    private UserService userService;
25  
26    private DateHelper dateHelper;
27  
28    @Inject
29    LabJournalEntryService(
30      LabJournalEntryDAO labJournalEntryDAO,
31      DataObjectService dataObjectService,
32      UserService userService,
33      DateHelper dateHelper
34    ) {
35      this.labJournalEntryDAO = labJournalEntryDAO;
36      this.dataObjectService = dataObjectService;
37      this.userService = userService;
38      this.dateHelper = dateHelper;
39    }
40  
41    public LabJournalEntry createLabJournalEntry(Long dataObjectId, String content, String userName) {
42      LabJournalEntry labJournalEntry = new LabJournalEntry();
43      User user = userService.getUser(userName);
44      DataObject dataObject = dataObjectService.getDataObjectByShepardId(dataObjectId);
45      labJournalEntry.setContent(content);
46      labJournalEntry.setCreatedBy(user);
47      labJournalEntry.setCreatedAt(dateHelper.getDate());
48      labJournalEntry.setDataObject(dataObject);
49      labJournalEntry = labJournalEntryDAO.createOrUpdate(labJournalEntry);
50      return labJournalEntry;
51    }
52  
53    public List<LabJournalEntry> getLabJournalEntries(DataObject dataObject) {
54      if (null == dataObject) return new ArrayList<LabJournalEntry>();
55      List<LabJournalEntry> labJournalEntries = dataObject.getLabJournalEntries();
56      List<Long> labJournalEntryIds = labJournalEntries.stream().map(LabJournalEntry::getId).collect(Collectors.toList());
57      labJournalEntries = labJournalEntryDAO.findLabJournalEntriesByIds(labJournalEntryIds);
58      return labJournalEntries
59        .stream()
60        .filter(labJournalEntry -> !labJournalEntry.isDeleted())
61        .sorted(Comparator.comparing(LabJournalEntry::getCreatedAt))
62        .collect(Collectors.toList());
63    }
64  
65    public LabJournalEntry getLabJournalEntry(long labJournalEntryId) {
66      return labJournalEntryDAO.findByNeo4jId(labJournalEntryId);
67    }
68  
69    public LabJournalEntry updateLabJournalEntry(long labJournalEntryId, String content, String userName) {
70      LabJournalEntry labJournalEntry = labJournalEntryDAO.findByNeo4jId(labJournalEntryId);
71      if (null == labJournalEntry) return null;
72      User user = userService.getUser(userName);
73      labJournalEntry.setContent(content);
74      labJournalEntry.setUpdatedAt(dateHelper.getDate());
75      labJournalEntry.setUpdatedBy(user);
76      labJournalEntry = labJournalEntryDAO.createOrUpdate(labJournalEntry);
77      return labJournalEntry;
78    }
79  
80    public boolean deleteLabJournalEntry(long labJournalEntryId, String userName) {
81      User user = userService.getUser(userName);
82      return labJournalEntryDAO.deleteLabJournalEntry(labJournalEntryId, user, dateHelper.getDate());
83    }
84  
85    public Long getCollectionId(Long labJournalEntryId) {
86      LabJournalEntry labJournalEntry = labJournalEntryDAO.findByNeo4jId(labJournalEntryId);
87      if (null == labJournalEntry) return null;
88      DataObject dataObject = dataObjectService.getDataObjectByShepardId(labJournalEntry.getDataObject().getId());
89      return dataObject.getCollection().getId();
90    }
91  }