/** * When an installation is created or updated, the categories are passed without IDs. * This method solve this issue by checking for existing categories and updating them (otherwise it would * persist a new object). * @param entity to merge the categories for * @param categoriesToMerge are the categories to merge with the existing one */ private void mergeCategories(Installation entity, Set<Category> categoriesToMerge) { if (entity.getCategories() != null) { final List<String> categoryNames = convertToNames(categoriesToMerge); final List<Category> existingCategoriesFromDB = categoryDao.findByNames(categoryNames); // Replace json dematerialised categories with their persistent counter parts (see Category.equals), // by remove existing/persistent categories from the new collection, and adding them back in (with their PK). categoriesToMerge.removeAll(existingCategoriesFromDB); categoriesToMerge.addAll(existingCategoriesFromDB); // and apply the passed in ones. entity.setCategories(categoriesToMerge); } }
@Override @Asynchronous public void unsubscribeOldTopics(Installation installation) { FCMTopicManager topicManager = new FCMTopicManager((AndroidVariant) installation.getVariant()); Set<String> oldCategories = topicManager.getSubscribedCategories(installation); // Remove current categories from the set of old ones oldCategories.removeAll(convertToNames(installation.getCategories())); // Remove global variant topic because we don't want to unsubscribe it oldCategories.remove(installation.getVariant().getVariantID()); for (String categoryName : oldCategories) { topicManager.unsubscribe(installation, categoryName); } }
private void storeInstallationAndSetReferences(Variant variant, Installation entity) { // ensure lower case for iOS if (variant.getType() == VariantType.IOS) { entity.setDeviceToken(entity.getDeviceToken().toLowerCase()); } // set reference entity.setVariant(variant); // update attached categories mergeCategories(entity, entity.getCategories()); // store Installation entity installationDao.create(entity); } }
@Override public void updateInstallation(Installation installationToUpdate, Installation postedInstallation) { // copy the "updateable" values: mergeCategories(installationToUpdate, postedInstallation.getCategories()); installationToUpdate.setDeviceToken(postedInstallation.getDeviceToken()); installationToUpdate.setAlias(postedInstallation.getAlias()); installationToUpdate.setDeviceType(postedInstallation.getDeviceType()); installationToUpdate.setOperatingSystem(postedInstallation .getOperatingSystem()); installationToUpdate.setOsVersion(postedInstallation.getOsVersion()); installationToUpdate.setEnabled(postedInstallation.isEnabled()); installationToUpdate.setPlatform(postedInstallation.getPlatform()); // update it: updateInstallation(installationToUpdate); // unsubscribe Android devices from topics that device should no longer be subscribed to if (installationToUpdate.getVariant().getType() == VariantType.ANDROID) { unsubscribeOldTopics(installationToUpdate); } }