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

113 lines
6.4 KiB
Java
Raw Normal View History

2017-12-15 00:01:26 +01:00
package eu.eudat.managers;
2018-03-19 13:40:04 +01:00
import eu.eudat.builders.entity.ContentBuilder;
2018-02-16 08:45:18 +01:00
import eu.eudat.builders.model.models.ProjectBuilder;
2018-03-19 13:40:04 +01:00
import eu.eudat.dao.entities.ContentDao;
2017-12-15 00:01:26 +01:00
import eu.eudat.dao.entities.ProjectDao;
2017-12-20 15:52:09 +01:00
import eu.eudat.dao.entities.UserInfoDao;
2018-03-19 13:40:04 +01:00
import eu.eudat.entities.Content;
import eu.eudat.exceptions.files.TempFileNotFoundException;
2018-02-09 16:54:41 +01:00
import eu.eudat.models.HintedModelFactory;
2017-12-22 09:31:05 +01:00
import eu.eudat.models.external.ExternalSourcesItemModel;
import eu.eudat.models.external.ProjectsExternalSourcesModel;
2018-03-19 13:40:04 +01:00
import eu.eudat.models.files.ContentFile;
2018-01-25 16:24:21 +01:00
import eu.eudat.models.helpers.common.DataTableData;
2017-12-15 00:01:26 +01:00
import eu.eudat.models.project.Project;
2017-12-19 09:38:47 +01:00
import eu.eudat.models.project.ProjectCriteriaRequest;
2018-01-17 13:03:51 +01:00
import eu.eudat.models.project.ProjectListingModel;
2017-12-15 00:01:26 +01:00
import eu.eudat.models.project.ProjectTableRequest;
2017-12-20 15:52:09 +01:00
import eu.eudat.models.security.Principal;
2017-12-22 09:31:05 +01:00
import eu.eudat.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
import eu.eudat.proxy.fetching.RemoteFetcher;
import eu.eudat.queryable.QueryableList;
2018-02-16 08:45:18 +01:00
import eu.eudat.services.ApiContext;
2018-03-19 13:40:04 +01:00
import eu.eudat.services.helpers.FileStorageService;
2017-12-15 00:01:26 +01:00
2018-03-19 13:40:04 +01:00
import java.io.IOException;
2018-02-16 11:34:02 +01:00
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
2017-12-15 00:01:26 +01:00
public class ProjectManager {
2018-03-19 13:40:04 +01:00
public DataTableData<eu.eudat.models.project.ProjectListingModel> getPaged(ProjectDao projectRepository, ProjectTableRequest projectTableRequest, Principal principal) throws Exception {
eu.eudat.entities.UserInfo userInfo = new eu.eudat.entities.UserInfo();
userInfo.setId(principal.getId());
2018-01-12 09:05:44 +01:00
QueryableList<eu.eudat.entities.Project> items = projectRepository.getWithCriteria(projectTableRequest.getCriteria());
2018-03-19 13:40:04 +01:00
QueryableList<eu.eudat.entities.Project> authItems = projectRepository.getAuthenticated(items, userInfo);
QueryableList<eu.eudat.entities.Project> pagedItems = PaginationManager.applyPaging(authItems, projectTableRequest);
DataTableData<eu.eudat.models.project.ProjectListingModel> dataTable = new DataTableData<>();
2018-02-07 10:56:30 +01:00
2018-02-21 11:07:31 +01:00
CompletableFuture projectsFuture = pagedItems.withHint(HintedModelFactory.getHint(ProjectListingModel.class)).selectAsync(item -> new ProjectListingModel().fromDataModel(item)).whenComplete((results, throwable) -> {
dataTable.setData(results);
2018-02-07 10:56:30 +01:00
});
CompletableFuture countFuture = pagedItems.countAsync().whenComplete((count, throwable) -> dataTable.setTotalCount(count));
2018-02-16 08:45:18 +01:00
CompletableFuture.allOf(projectsFuture, countFuture).join();
2018-01-12 09:05:44 +01:00
return dataTable;
}
2017-12-19 09:38:47 +01:00
2018-01-12 09:05:44 +01:00
public eu.eudat.models.project.Project getSingle(ProjectDao projectRepository, String id) throws InstantiationException, IllegalAccessException {
eu.eudat.models.project.Project project = new eu.eudat.models.project.Project();
project.fromDataModel(projectRepository.find(UUID.fromString(id)));
return project;
}
2017-12-19 09:38:47 +01:00
2018-01-12 09:05:44 +01:00
public eu.eudat.entities.Project inactivate(ProjectDao projectRepository, String id) throws InstantiationException, IllegalAccessException {
eu.eudat.entities.Project project = projectRepository.find(UUID.fromString(id));
2018-01-19 12:11:22 +01:00
project.setStatus(eu.eudat.entities.Project.Status.DELETED.getValue());
2018-01-12 09:05:44 +01:00
project = projectRepository.createOrUpdate(project);
return project;
}
2017-12-20 15:52:09 +01:00
2018-02-16 08:45:18 +01:00
public List<eu.eudat.models.project.Project> getCriteriaWithExternal(ApiContext apiContext, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
2018-03-05 17:18:45 +01:00
QueryableList<eu.eudat.entities.Project> items = apiContext.getOperationsContext().getDatabaseRepository().getProjectDao().getWithCriteria(projectCriteria.getCriteria());
2018-02-21 11:07:31 +01:00
List<eu.eudat.models.project.Project> projects = items.select(item -> new Project().fromDataModel(item));
2018-01-12 09:05:44 +01:00
List<Map<String, String>> remoteRepos = remoteFetcher.getProjects(projectCriteria.getCriteria().getLike());
ProjectsExternalSourcesModel projectsExternalSourcesModel = new ProjectsExternalSourcesModel().fromExternalItem(remoteRepos);
for (ExternalSourcesItemModel externalListingItem : projectsExternalSourcesModel) {
2018-03-05 17:18:45 +01:00
eu.eudat.models.project.Project project = apiContext.getOperationsContext().getBuilderFactory().getBuilder(ProjectBuilder.class)
2018-02-16 08:45:18 +01:00
.reference(externalListingItem.getRemoteId()).label(externalListingItem.getName())
.description(externalListingItem.getDescription()).uri(externalListingItem.getUri())
.abbreviation(externalListingItem.getAbbreviation()).status(eu.eudat.entities.Project.Status.fromInteger(0))
.build();
2018-01-12 09:05:44 +01:00
projects.add(project);
}
2018-01-17 13:03:51 +01:00
2018-01-12 09:05:44 +01:00
return projects;
}
2017-12-22 14:42:47 +01:00
2018-01-12 09:05:44 +01:00
public List<eu.eudat.models.project.Project> getCriteria(ProjectDao projectRepository, ProjectCriteriaRequest projectCriteria, RemoteFetcher remoteFetcher) throws IllegalAccessException, InstantiationException, HugeResultSet, NoURLFound {
QueryableList<eu.eudat.entities.Project> items = projectRepository.getWithCriteria(projectCriteria.getCriteria());
2018-02-21 11:07:31 +01:00
List<eu.eudat.models.project.Project> projects = items.select(item -> new Project().fromDataModel(item));
2018-01-12 09:05:44 +01:00
return projects;
}
2018-03-19 13:40:04 +01:00
public static void createOrUpdate(FileStorageService fileStorageService, ProjectDao projectRepository, ContentDao contentRepository, UserInfoDao userInfoRepository, eu.eudat.models.project.Project project, Principal principal) throws ParseException, IOException {
2018-01-12 09:05:44 +01:00
eu.eudat.entities.Project projectEntity = project.toDataModel();
2018-03-19 13:40:04 +01:00
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(contentRepository.createOrUpdate(content));
} catch (TempFileNotFoundException e) {
continue;
}
}
2018-01-12 09:05:44 +01:00
projectEntity.setCreationUser(userInfoRepository.find(principal.getId()));
projectRepository.createOrUpdate(projectEntity);
}
2017-12-15 00:01:26 +01:00
}