68 lines
3.0 KiB
Java
68 lines
3.0 KiB
Java
package eu.eudat.logic.managers;
|
|
|
|
import eu.eudat.data.dao.criteria.GrantCriteria;
|
|
import eu.eudat.data.entities.*;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.logic.services.operations.DatabaseRepository;
|
|
import eu.eudat.models.data.dmp.DataManagementPlan;
|
|
import eu.eudat.models.data.security.Principal;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
import java.text.ParseException;
|
|
|
|
@Component
|
|
public class QuickWizardManager {
|
|
|
|
private ApiContext apiContext;
|
|
private DatabaseRepository databaseRepository;
|
|
|
|
@Autowired
|
|
public QuickWizardManager(ApiContext apiContext) {
|
|
this.apiContext = apiContext;
|
|
this.databaseRepository = apiContext.getOperationsContext().getDatabaseRepository();
|
|
}
|
|
|
|
public Grant createOrUpdate(eu.eudat.models.data.grant.Grant grant, Principal principal) throws ParseException, IOException {
|
|
eu.eudat.data.entities.Grant grantEntity = grant.toDataModel();
|
|
grantEntity.setType(eu.eudat.data.entities.Grant.GrantType.INTERNAL.getValue());
|
|
grantEntity.setCreationUser(databaseRepository.getUserInfoDao().find(principal.getId()));
|
|
Grant grantEntityRet = databaseRepository.getGrantDao().createOrUpdate(grantEntity);
|
|
return grantEntityRet;
|
|
}
|
|
|
|
public DMP createOrUpdate(DataManagementPlan dataManagementPlan, Principal principal) throws Exception {
|
|
DMP newDmp = dataManagementPlan.toDataModel();
|
|
UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
|
createGrantIfItDoesntExist(newDmp, user);
|
|
//newDmp.setCreator(user);
|
|
DMP dmpret = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
|
|
if (dataManagementPlan.getAssociatedUsers().size() == 0)
|
|
assignUser(newDmp, user, apiContext);
|
|
return dmpret;
|
|
}
|
|
|
|
private void assignUser(DMP dmp, UserInfo userInfo, ApiContext apiContext) {
|
|
UserDMP userDMP = new UserDMP();
|
|
userDMP.setDmp(dmp);
|
|
userDMP.setUser(userInfo);
|
|
userDMP.setRole(UserDMP.UserDMPRoles.OWNER.getValue());
|
|
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
|
|
}
|
|
|
|
private void createGrantIfItDoesntExist(DMP newDmp, UserInfo userInfo) {
|
|
if (newDmp.getGrant() != null) {
|
|
Grant grant = newDmp.getGrant();
|
|
GrantCriteria criteria = new GrantCriteria();
|
|
criteria.setReference(grant.getReference());
|
|
eu.eudat.data.entities.Grant grantEntity = databaseRepository.getGrantDao().getWithCriteria(criteria).getSingleOrDefault();
|
|
if (grantEntity != null) grant.setId(grantEntity.getId());
|
|
else {
|
|
grant.setType(Grant.GrantType.EXTERNAL.getValue());
|
|
databaseRepository.getGrantDao().createOrUpdate(grant);
|
|
}
|
|
}
|
|
}
|
|
}
|