argos/dmp-backend/web/src/main/java/eu/eudat/logic/managers/ProjectManager.java

165 lines
9.7 KiB
Java

package eu.eudat.logic.managers;
import eu.eudat.exceptions.project.ProjectWithDMPsDeleteException;
import eu.eudat.logic.builders.entity.ContentBuilder;
import eu.eudat.logic.builders.model.models.ProjectBuilder;
import eu.eudat.data.dao.entities.ContentDao;
import eu.eudat.data.dao.entities.ProjectDao;
import eu.eudat.data.dao.entities.UserInfoDao;
import eu.eudat.data.entities.Content;
import eu.eudat.data.entities.DMP;
import eu.eudat.exceptions.files.TempFileNotFoundException;
import eu.eudat.logic.services.operations.DatabaseRepository;
import eu.eudat.models.HintedModelFactory;
import eu.eudat.models.data.external.ExternalSourcesItemModel;
import eu.eudat.models.data.external.ProjectsExternalSourcesModel;
import eu.eudat.models.data.files.ContentFile;
import eu.eudat.models.data.helpers.common.DataTableData;
import eu.eudat.models.data.project.Project;
import eu.eudat.data.query.items.item.project.ProjectCriteriaRequest;
import eu.eudat.models.data.project.ProjectListingModel;
import eu.eudat.data.query.items.table.project.ProjectTableRequest;
import eu.eudat.models.data.security.Principal;
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
import eu.eudat.logic.proxy.fetching.RemoteFetcher;
import eu.eudat.queryable.QueryableList;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.services.helpers.FileStorageService;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
@Component
public class ProjectManager {
private ApiContext apiContext;
private DatabaseRepository databaseRepository;
private FileStorageService fileStorageService;
private RemoteFetcher remoteFetcher;
public ProjectManager(ApiContext apiContext) {
this.apiContext = apiContext;
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
this.fileStorageService = apiContext.getOperationsContext().getFileStorageService();
this.remoteFetcher = apiContext.getOperationsContext().getRemoteFetcher();
}
public DataTableData<eu.eudat.models.data.project.ProjectListingModel> getPaged(ProjectTableRequest projectTableRequest, Principal principal, String fieldsGroup) throws Exception {
eu.eudat.data.entities.UserInfo userInfo = new eu.eudat.data.entities.UserInfo();
userInfo.setId(principal.getId());
ProjectDao projectRepository = databaseRepository.getProjectDao();
QueryableList<eu.eudat.data.entities.Project> items = projectRepository.getWithCriteria(projectTableRequest.getCriteria());
QueryableList<eu.eudat.data.entities.Project> authItems = projectRepository.getAuthenticated(items, userInfo);
QueryableList<eu.eudat.data.entities.Project> pagedItems = PaginationManager.applyPaging(authItems, projectTableRequest);
DataTableData<eu.eudat.models.data.project.ProjectListingModel> dataTable = new DataTableData<>();
CompletableFuture projectsFuture;
if (fieldsGroup.equals("listing")) {
projectsFuture = pagedItems.withHint(HintedModelFactory.getHint(ProjectListingModel.class)).selectAsync(item -> {
item.setDmps(item.getDmps().stream().filter(
dmp -> dmp.getCreator().getId().equals(principal.getId()) || dmp.getUsers().stream().filter(user -> user.getId().equals(principal.getId())).collect(Collectors.toList()).size() > 0)
.collect(Collectors.groupingBy(DMP::getGroupId))
.values().stream()
.map(dmps -> dmps.stream().reduce((first, second) -> {
if (first.getVersion() > second.getVersion()) return first;
else return second;
}).get())
.collect(Collectors.toSet()));
return new ProjectListingModel().fromDataModelWIthDmps(item);
}).whenComplete((results, throwable) -> {
dataTable.setData(results);
});
} else {
projectsFuture = pagedItems.selectAsync(item -> new ProjectListingModel().fromDataModel(item))
.whenComplete((results, throwable) -> {
dataTable.setData(results);
});
}
CompletableFuture countFuture = pagedItems.countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count));
CompletableFuture.allOf(projectsFuture, countFuture).join();
return dataTable;
}
public eu.eudat.models.data.project.Project getSingle(String id) throws InstantiationException, IllegalAccessException {
eu.eudat.models.data.project.Project project = new eu.eudat.models.data.project.Project();
project.fromDataModel(databaseRepository.getProjectDao().find(UUID.fromString(id)));
return project;
}
public eu.eudat.data.entities.Project inactivate(String id) throws InstantiationException, IllegalAccessException {
ProjectDao projectRepository = databaseRepository.getProjectDao();
eu.eudat.data.entities.Project project = projectRepository.find(UUID.fromString(id));
project.setStatus(eu.eudat.data.entities.Project.Status.DELETED.getValue());
project = projectRepository.createOrUpdate(project);
return project;
}
public List<eu.eudat.models.data.project.Project> getCriteriaWithExternal(ProjectCriteriaRequest projectCriteria, Principal principal) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
eu.eudat.data.entities.UserInfo userInfo = new eu.eudat.data.entities.UserInfo();
userInfo.setId(principal.getId());
QueryableList<eu.eudat.data.entities.Project> items = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getWithCriteria(projectCriteria.getCriteria());
QueryableList<eu.eudat.data.entities.Project> authItems = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getAuthenticated(items, userInfo);
List<eu.eudat.models.data.project.Project> projects = authItems.select(item -> new Project().fromDataModel(item));
List<Map<String, String>> remoteRepos = remoteFetcher.getProjects(projectCriteria.getCriteria().getLike());
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos);
for (ExternalSourcesItemModel externalListingItem : projectsExternalSourcesModel) {
eu.eudat.models.data.project.Project project = apiContext.getOperationsContext().getBuilderFactory().getBuilder(ProjectBuilder.class)
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
.description(externalListingItem.getDescription()).uri(externalListingItem.getUri())
.abbreviation(externalListingItem.getAbbreviation()).status(eu.eudat.data.entities.Project.Status.fromInteger(0))
.build();
projects.add(project);
}
projects.sort(Comparator.comparing(x -> x.getLabel()));
return projects;
}
public List<eu.eudat.models.data.project.Project> getCriteria(ProjectCriteriaRequest projectCriteria) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
ProjectDao projectRepository = databaseRepository.getProjectDao();
QueryableList<eu.eudat.data.entities.Project> items = projectRepository.getWithCriteria(projectCriteria.getCriteria());
if (projectCriteria.getLength() != null) items.take(projectCriteria.getLength());
List<eu.eudat.models.data.project.Project> projects = items.select(item -> new Project().fromDataModel(item));
return projects;
}
public void createOrUpdate(eu.eudat.models.data.project.Project project, Principal principal) throws ParseException, IOException {
eu.eudat.data.entities.Project projectEntity = project.toDataModel();
if (project.getFiles() != null) {
for (ContentFile file : project.getFiles()) {
try {
ContentFile storedFile = fileStorageService.copyFromTempFileSystem(file);
Content content = new ContentBuilder().extension(file.getType())
.label(file.getFilename())
.locationType(Content.LocationType.INTERNAL.getValue())
.parentType(Content.ParentType.PROJECT.getValue())
.uri("LOCAL:" + storedFile.getId())
.build();
projectEntity.setContent(databaseRepository.getContentDao().createOrUpdate(content));
} catch (TempFileNotFoundException e) {
continue;
}
}
}
projectEntity.setType(eu.eudat.data.entities.Project.ProjectType.INTERNAL.getValue());
projectEntity.setCreationUser(databaseRepository.getUserInfoDao().find(principal.getId()));
databaseRepository.getProjectDao().createOrUpdate(projectEntity);
}
public void delete(UUID uuid) {
eu.eudat.data.entities.Project oldProject = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().find(uuid);
if (oldProject.getDmps().size() > 0)
throw new ProjectWithDMPsDeleteException("You cannot Remove Projects with DMPs");
oldProject.setStatus(DMP.DMPStatus.DELETED.getValue());
apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().createOrUpdate(oldProject);
}
}