diff --git a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/PreferencesServiceImpl.java b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/PreferencesServiceImpl.java index f9806321d1e..96b7d995d1a 100644 --- a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/PreferencesServiceImpl.java +++ b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/PreferencesServiceImpl.java @@ -18,6 +18,7 @@ package de.tudarmstadt.ukp.inception.preferences; import static de.tudarmstadt.ukp.inception.support.json.JSONUtil.toJsonString; +import static java.util.Objects.requireNonNull; import java.io.IOException; import java.lang.invoke.MethodHandles; @@ -66,11 +67,14 @@ public PreferencesServiceImpl(EntityManager aEntityManager) @Transactional public T loadTraitsForUser(Key aKey, User aUser) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + try { - Optional preference = getRawUserPreference(aKey, aUser); + var preference = getRawUserPreference(aKey, aUser); if (preference.isPresent()) { - String json = preference.get().getTraits(); - T result = JSONUtil.fromJsonString(aKey.getTraitClass(), json); + var json = preference.get().getTraits(); + var result = JSONUtil.fromJsonString(aKey.getTraitClass(), json); LOG.debug("Loaded preferences for key {} and user {}: [{}]", aKey, aUser, result); return result; } @@ -89,9 +93,12 @@ public T loadTraitsForUser(Key aKey, User aUser) @Transactional public void saveTraitsForUser(Key aKey, User aUser, T aTraits) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + requireNonNull(aTraits, "Parameter [traits] must be specified"); + try { - UserPreference preference = getRawUserPreference(aKey, aUser) - .orElseGet(UserPreference::new); + var preference = getRawUserPreference(aKey, aUser).orElseGet(UserPreference::new); preference.setUser(aUser); preference.setName(aKey.getName()); preference.setTraits(toJsonString(aTraits)); @@ -106,6 +113,9 @@ public void saveTraitsForUser(Key aKey, User aUser, T aTraits) private Optional getRawUserPreference(Key aKey, User aUser) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + var query = String.join("\n", // "FROM UserPreference ", // "WHERE user = :user ", // @@ -151,6 +161,10 @@ public Optional loadOptionalTraitsForUserAndProject(Key aKey, User aUs @Transactional public T loadTraitsForUserAndProject(Key aKey, User aUser, Project aProject) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + try { var pref = getUserProjectPreference(aKey, aUser, aProject); if (pref.isPresent()) { @@ -175,8 +189,13 @@ public T loadTraitsForUserAndProject(Key aKey, User aUser, Project aProje public void saveTraitsForUserAndProject(Key aKey, User aUser, Project aProject, T aTraits) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + requireNonNull(aTraits, "Parameter [traits] must be specified"); + try { - UserProjectPreference preference = getUserProjectPreference(aKey, aUser, aProject) + var preference = getUserProjectPreference(aKey, aUser, aProject) .orElseGet(UserProjectPreference::new); preference.setUser(aUser); preference.setProject(aProject); @@ -195,14 +214,18 @@ public void saveTraitsForUserAndProject(Key aKey, User aUser, Project aPr private Optional getUserProjectPreference(Key aKey, User aUser, Project aProject) { - String query = String.join("\n", // + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aUser, "Parameter [user] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + + var query = String.join("\n", // "FROM UserProjectPreference ", // "WHERE user = :user", // "AND project = :project", // "AND name = :name"); try { - UserProjectPreference pref = entityManager // + var pref = entityManager // .createQuery(query, UserProjectPreference.class) // .setParameter("user", aUser) // .setParameter("project", aProject) // @@ -220,16 +243,14 @@ private Optional getUserProjectPreference(Key aKey @Transactional public List listUserPreferencesForProject(Project aProject) { + requireNonNull(aProject, "Parameter [project] must be specified"); var builder = entityManager.getCriteriaBuilder(); var query = builder.createQuery(UserProjectPreference.class); var root = query.from(UserProjectPreference.class); query.select(root); - query.where( // - builder.equal(root.get(UserProjectPreference_.project), aProject), // - // TODO We have (had) a bug somewhere that wrote nulls to the USER column - builder.isNotNull(root.get(UserProjectPreference_.user))); + query.where(builder.equal(root.get(UserProjectPreference_.project), aProject)); return entityManager.createQuery(query).getResultList(); } @@ -238,6 +259,8 @@ public List listUserPreferencesForProject(Project aProjec @Transactional public void saveUserProjectPreference(UserProjectPreference aPreference) { + requireNonNull(aPreference, "Parameter [preference] must be specified"); + entityManager.persist(aPreference); } @@ -245,11 +268,14 @@ public void saveUserProjectPreference(UserProjectPreference aPreference) @Transactional public T loadDefaultTraitsForProject(Key aKey, Project aProject) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + try { - Optional pref = getDefaultProjectPreference(aKey, aProject); + var pref = getDefaultProjectPreference(aKey, aProject); if (pref.isPresent()) { - String json = pref.get().getTraits(); - T result = JSONUtil.fromJsonString(aKey.getTraitClass(), json); + var json = pref.get().getTraits(); + var result = JSONUtil.fromJsonString(aKey.getTraitClass(), json); LOG.debug("Loaded default preferences for key {} and project {}: [{}]", aKey, aProject, result); return result; @@ -269,8 +295,11 @@ public T loadDefaultTraitsForProject(Key aKey, Project aProject) @Transactional public void saveDefaultTraitsForProject(Key aKey, Project aProject, T aTraits) { + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + try { - DefaultProjectPreference preference = getDefaultProjectPreference(aKey, aProject) + var preference = getDefaultProjectPreference(aKey, aProject) .orElseGet(DefaultProjectPreference::new); preference.setProject(aProject); preference.setName(aKey.getName()); @@ -288,7 +317,10 @@ public void saveDefaultTraitsForProject(Key aKey, Project aProject, T aTr @Override public void clearDefaultTraitsForProject(Key aKey, Project aProject) { - String query = String.join("\n", // + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + + var query = String.join("\n", // "DELETE DefaultProjectPreference ", // "WHERE project = :project", // "AND name = :name"); @@ -302,13 +334,16 @@ public void clearDefaultTraitsForProject(Key aKey, Project aProject) private Optional getDefaultProjectPreference(Key aKey, Project aProject) { - String query = String.join("\n", // + requireNonNull(aKey, "Parameter [key] must be specified"); + requireNonNull(aProject, "Parameter [project] must be specified"); + + var query = String.join("\n", // "FROM DefaultProjectPreference ", // "WHERE project = :project", // "AND name = :name"); try { - DefaultProjectPreference pref = entityManager // + var pref = entityManager // .createQuery(query, DefaultProjectPreference.class) // .setParameter("project", aProject) // .setParameter("name", aKey.getName()) // @@ -325,7 +360,9 @@ private Optional getDefaultProjectPreference(Key listDefaultTraitsForProject(Project aProject) { - String query = String.join("\n", // + requireNonNull(aProject, "Parameter [project] must be specified"); + + var query = String.join("\n", // "FROM DefaultProjectPreference ", // "WHERE project = :project"); @@ -339,26 +376,31 @@ public List listDefaultTraitsForProject(Project aProje @Transactional public void saveDefaultProjectPreference(DefaultProjectPreference aPreference) { + requireNonNull(aPreference, "Parameter [preference] must be specified"); + entityManager.persist(aPreference); } /* * Use default constructor of aClass to create new instance of T */ + @SuppressWarnings("unchecked") private T buildDefault(Class aClass) { + requireNonNull(aClass, "Parameter [class] must be specified"); + try { return aClass.getConstructor().newInstance(); } catch (NoSuchMethodException e) { if (Map.class.isAssignableFrom(aClass)) { - return (T) new LinkedHashMap(); + return (T) new LinkedHashMap<>(); } - return ExceptionUtils.rethrow(e); + return ExceptionUtils.wrapAndThrow(e); } catch (Exception e) { - return ExceptionUtils.rethrow(e); + return ExceptionUtils.wrapAndThrow(e); } } } diff --git a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/DefaultProjectPreferencesExporter.java b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/DefaultProjectPreferencesExporter.java index f43cc5958d2..b621691e440 100644 --- a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/DefaultProjectPreferencesExporter.java +++ b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/DefaultProjectPreferencesExporter.java @@ -19,7 +19,6 @@ import java.io.File; import java.util.ArrayList; -import java.util.List; import java.util.zip.ZipFile; import org.slf4j.Logger; @@ -61,12 +60,11 @@ public DefaultProjectPreferencesExporter(PreferencesService aPreferencesService) public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, ExportedProject aExProject, File aFile) { - Project project = aRequest.getProject(); + var project = aRequest.getProject(); - List exportedDefaultPreferences = new ArrayList<>(); - for (DefaultProjectPreference defaultPreference : preferencesService - .listDefaultTraitsForProject(project)) { - ExportedDefaultProjectPreference exportedDefaultPreference = new ExportedDefaultProjectPreference(); + var exportedDefaultPreferences = new ArrayList<>(); + for (var defaultPreference : preferencesService.listDefaultTraitsForProject(project)) { + var exportedDefaultPreference = new ExportedDefaultProjectPreference(); exportedDefaultPreference.setName(defaultPreference.getName()); exportedDefaultPreference.setTraits(defaultPreference.getTraits()); exportedDefaultPreferences.add(exportedDefaultPreference); @@ -81,11 +79,11 @@ public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonit public void importData(ProjectImportRequest aRequest, Project aProject, ExportedProject aExProject, ZipFile aZip) { - ExportedDefaultProjectPreference[] exportedDefaultPreferences = aExProject - .getArrayProperty(KEY, ExportedDefaultProjectPreference.class); + var exportedDefaultPreferences = aExProject.getArrayProperty(KEY, + ExportedDefaultProjectPreference.class); - for (ExportedDefaultProjectPreference exportedDefaultPreference : exportedDefaultPreferences) { - DefaultProjectPreference defaultPreference = new DefaultProjectPreference(); + for (var exportedDefaultPreference : exportedDefaultPreferences) { + var defaultPreference = new DefaultProjectPreference(); defaultPreference.setProject(aProject); defaultPreference.setName(exportedDefaultPreference.getName()); defaultPreference.setTraits(exportedDefaultPreference.getTraits()); diff --git a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/UserProjectPreferencesExporter.java b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/UserProjectPreferencesExporter.java index 800b63d09d7..ad478389a01 100644 --- a/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/UserProjectPreferencesExporter.java +++ b/inception/inception-preferences/src/main/java/de/tudarmstadt/ukp/inception/preferences/exporter/UserProjectPreferencesExporter.java @@ -36,7 +36,6 @@ import de.tudarmstadt.ukp.clarin.webanno.model.Project; import de.tudarmstadt.ukp.clarin.webanno.project.exporters.ProjectPermissionsExporter; import de.tudarmstadt.ukp.clarin.webanno.security.UserDao; -import de.tudarmstadt.ukp.clarin.webanno.security.model.User; import de.tudarmstadt.ukp.inception.preferences.PreferencesService; import de.tudarmstadt.ukp.inception.preferences.config.PreferencesServiceAutoConfig; import de.tudarmstadt.ukp.inception.preferences.model.UserProjectPreference; @@ -74,12 +73,11 @@ public List> getImportDependencies() public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonitor aMonitor, ExportedProject aExProject, File aFile) { - Project project = aRequest.getProject(); + var project = aRequest.getProject(); - List exportedDefaultPreferences = new ArrayList<>(); - for (UserProjectPreference userPreference : preferencesService - .listUserPreferencesForProject(project)) { - ExportedUserProjectPreference exportedDefaultPreference = new ExportedUserProjectPreference(); + var exportedDefaultPreferences = new ArrayList(); + for (var userPreference : preferencesService.listUserPreferencesForProject(project)) { + var exportedDefaultPreference = new ExportedUserProjectPreference(); exportedDefaultPreference.setUser(userPreference.getUser().getUsername()); exportedDefaultPreference.setName(userPreference.getName()); exportedDefaultPreference.setTraits(userPreference.getTraits()); @@ -95,19 +93,19 @@ public void exportData(FullProjectExportRequest aRequest, ProjectExportTaskMonit public void importData(ProjectImportRequest aRequest, Project aProject, ExportedProject aExProject, ZipFile aZip) { - ExportedUserProjectPreference[] exportedDefaultPreferences = aExProject - .getArrayProperty(KEY, ExportedUserProjectPreference.class); + var exportedDefaultPreferences = aExProject.getArrayProperty(KEY, + ExportedUserProjectPreference.class); - int importedPreferences = 0; - int missingUsers = 0; - for (ExportedUserProjectPreference exportedDefaultPreference : exportedDefaultPreferences) { - User user = userRepository.get(exportedDefaultPreference.getUser()); + var importedPreferences = 0; + var missingUsers = 0; + for (var exportedDefaultPreference : exportedDefaultPreferences) { + var user = userRepository.get(exportedDefaultPreference.getUser()); if (user == null) { missingUsers++; continue; } - UserProjectPreference userPreference = new UserProjectPreference(); + var userPreference = new UserProjectPreference(); userPreference.setProject(aProject); userPreference.setUser(user); userPreference.setName(exportedDefaultPreference.getName()); diff --git a/inception/inception-preferences/src/main/resources/de/tudarmstadt/ukp/inception/preferences/model/db-changelog.xml b/inception/inception-preferences/src/main/resources/de/tudarmstadt/ukp/inception/preferences/model/db-changelog.xml index 3bdab4b8896..6ce5005e232 100644 --- a/inception/inception-preferences/src/main/resources/de/tudarmstadt/ukp/inception/preferences/model/db-changelog.xml +++ b/inception/inception-preferences/src/main/resources/de/tudarmstadt/ukp/inception/preferences/model/db-changelog.xml @@ -115,4 +115,38 @@ constraintName="UK_default_project_preference_name_project" tableName="default_project_preference" columnNames="project, name" /> - \ No newline at end of file + + + + + + + user IS NULL + + + project IS NULL + + + + + + + + + + + user IS NULL + + + + + + + + + + project IS NULL + + + +