ApplicationUpgrades.java
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.component.listener;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys.server.component.security.AsAdminInvoker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.TransactionTemplate;
/**
* Declare sorts of things that upgrade the existing database
*
* @author Matija Obreza
* @since 2.4
*/
@Component
public class ApplicationUpgrades implements InitializingBean {
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(ApplicationUpgrades.class);
@Autowired
protected AsAdminInvoker asAdminInvoker;
@Autowired
protected CurrentApplicationContext springContext;
@Autowired
private PlatformTransactionManager transactionManager;
private TransactionTemplate transactionTemplate;
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
LOG.info("Executing {}", this.getClass().getName());
this.transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.setReadOnly(false);
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);
// aclEnsureClassOIDs();
// aclMakePartnersPublic();
// upgradeShortFiltrerCodeFromV1ToV2();
// aclMakePartnerSIDs();
// aclUpdateCatalogSecurity();
}
public interface IAction {
void run() throws Exception;
}
// private void executeInTransaction(IAction action) {
// transactionTemplate.execute(new TransactionCallbackWithoutResult() {
// protected void doInTransactionWithoutResult(TransactionStatus status) {
// try {
// action.run();
// } catch (Throwable e) {
// status.setRollbackOnly();
// LOG.error("Error execution application upgrade: {}", e.getMessage(), e);
// }
// }
// });
// }
// @Autowired
// private ShortFilterRepository shortFilterRepository;
// @Autowired
// private ShortFilterService shortFilterService;
//
// private void upgradeShortFiltrerCodeFromV1ToV2() throws Exception {
// asAdminInvoker.invoke(() -> {
// List<ShortFilter> allShortFilters = shortFilterRepository.findAll();
// allShortFilters.forEach((filter) -> {
// if (filter.getCode().startsWith(ShortFilterServiceImpl.CODE_PREFIX_V1)) {
// shortFilterService.upgradeFilterCode(filter);
// }
// });
//
// return true;
// });
// }
// @Autowired
// private CustomAclService aclService;
// private void aclEnsureClassOIDs() throws Exception {
// asAdminInvoker.invoke(() -> {
// LOG.warn("Making some Entities publicly readable by default");
//
// for (Class<? extends AclAwareModel> clazz : Lists.newArrayList(Partner.class,
// FaoInstitute.class)) {
// LOG.warn("Making {} publicly readable by default", clazz.getName());
// aclService.createOrUpdatePermissions(ClassAclOid.forClass(clazz));
// aclService.makePubliclyReadable(ClassAclOid.forClass(clazz), true);
// }
//
// return true;
// });
// }
// @Autowired
// private PartnerRepository partnerRepository;
//
// private void aclMakePartnersPublic() throws Exception {
// asAdminInvoker.invoke(() -> {
// LOG.warn("Making Partners publicly readable");
//
// partnerRepository.findAll().forEach(partner -> {
// LOG.warn("Making Partner {} publicly readable", partner.getShortName());
// aclService.createOrUpdatePermissions(partner);
// });
// return true;
// });
// }
// @Autowired
// private PartnerRepository partnerRepository;
//
// @Autowired
// private CustomAclService aclService;
//
// private void aclMakePartnerSIDs() throws Exception {
// asAdminInvoker.invoke(() -> {
// partnerRepository.findAll().forEach(partner -> {
// LOG.warn("Ensuring ACL SID for Partner {}", partner.getShortName());
// executeInTransaction(() -> {
// aclService.ensureAuthoritySid(partner.getAuthorityName());
// });
// });
// return true;
// });
// }
// @Autowired
// private DatasetRepository datasetRepository;
// @Autowired
// private SubsetRepository subsetRepository;
// @Autowired
// private DescriptorRepository descriptorRepository;
// @Autowired
// private DescriptorListRepository descriptorListRepository;
// @Autowired
// private DatasetService datasetService;
// @Autowired
// private RepositoryService repositoryService;
//
// private void aclUpdateCatalogSecurity() throws Exception {
// asAdminInvoker.invoke(() -> {
// datasetRepository.findAll().forEach(dataset -> {
// executeInTransaction(() -> {
// Dataset ds = datasetRepository.findOne(dataset.getId());
// LOG.warn("Fixing ACL for dataset {}", ds.getTitle());
// aclService.setAclParent(ds, null);
// if (ds.getOwner() == null) {
// LOG.warn("No owner for id={} {}", ds.getId(), ds.getTitle());
// return;
// }
// final AclSid sid =
// aclService.getAuthoritySid(ds.getOwner().getAuthorityName());
// aclService.setPermissions(ds, sid, new Permissions().grantAll());
// aclService.makePubliclyReadable(ds, ds.isPublished());
//
// Path dsPath = datasetService.getDatasetRepositoryFolder(ds);
// RepositoryFolder folder = repositoryService.getFolder(dsPath);
// if (folder != null) {
// aclService.setAclParent(folder, ds);
// // aclService.makePubliclyReadable(folder, dataset.isPublished());
// }
// });
// });
// return true;
// });
//
// asAdminInvoker.invoke(() -> {
// executeInTransaction(() -> {
// subsetRepository.findAll().forEach(subset -> {
// LOG.warn("Fixing ACL for subset {}", subset.getTitle());
// aclService.setAclParent(subset, null);
// final AclSid sid =
// aclService.getAuthoritySid(subset.getOwner().getAuthorityName());
// aclService.setPermissions(subset, sid, new Permissions().grantAll());
// aclService.makePubliclyReadable(subset, subset.isPublished());
// });
// });
// return true;
// });
//
// asAdminInvoker.invoke(() -> {
// descriptorRepository.findAll().forEach(descriptor -> {
// executeInTransaction(() -> {
// Descriptor d = descriptorRepository.findOne(descriptor.getId());
// LOG.warn("Fixing ACL for descriptor {}", d.getTitle());
// aclService.setAclParent(d, null);
// final AclSid sid =
// aclService.getAuthoritySid(d.getOwner().getAuthorityName());
// aclService.setPermissions(d, sid, new Permissions().grantAll());
// aclService.makePubliclyReadable(d, d.isPublished());
// });
// });
// return true;
// });
//
// asAdminInvoker.invoke(() -> {
// executeInTransaction(() -> {
// descriptorListRepository.findAll().forEach(descriptorList -> {
// LOG.warn("Fixing ACL for descriptor list {}", descriptorList.getTitle());
// aclService.setAclParent(descriptorList, null);
// final AclSid sid =
// aclService.getAuthoritySid(descriptorList.getOwner().getAuthorityName());
// aclService.setPermissions(descriptorList, sid, new Permissions().grantAll());
// aclService.makePubliclyReadable(descriptorList,
// descriptorList.isPublished());
// });
// });
// return true;
// });
// }
}