001 package org.cumulus4j.store.datastoreversion.command; 002 003 import java.util.Collection; 004 import java.util.Set; 005 006 import javax.jdo.PersistenceManager; 007 import javax.jdo.Query; 008 009 import org.cumulus4j.store.Cumulus4jStoreManager; 010 import org.cumulus4j.store.crypto.CryptoContext; 011 import org.cumulus4j.store.datastoreversion.AbstractDatastoreVersionCommand; 012 import org.cumulus4j.store.datastoreversion.CommandApplyParam; 013 import org.cumulus4j.store.model.DataEntry; 014 import org.cumulus4j.store.model.IndexEntry; 015 016 public class IntroduceKeyStoreRefID extends AbstractDatastoreVersionCommand 017 { 018 private CryptoContext cryptoContext; 019 private Set<Class<? extends IndexEntry>> indexEntryClasses; 020 021 @Override 022 public int getCommandVersion() { 023 return 0; 024 } 025 026 @Override 027 public void apply(CommandApplyParam commandApplyParam) { 028 PersistenceManager pm = commandApplyParam.getPersistenceManager(); 029 cryptoContext = commandApplyParam.getCryptoContext(); 030 031 if (pm == cryptoContext.getPersistenceManagerForData()) 032 applyToData(commandApplyParam, pm); 033 034 if (pm == cryptoContext.getPersistenceManagerForIndex()) 035 applyToIndex(commandApplyParam, pm); 036 } 037 038 @SuppressWarnings("deprecation") 039 protected void applyToData(CommandApplyParam commandApplyParam, PersistenceManager pm) { 040 Collection<DataEntry> dataEntries = getAll(pm, DataEntry.class); 041 for (DataEntry dataEntry : dataEntries) { 042 dataEntry.setKeyStoreRefID(commandApplyParam.getCryptoContext().getKeyStoreRefID()); 043 } 044 } 045 046 protected void applyToIndex(CommandApplyParam commandApplyParam, PersistenceManager pm) { 047 for (Class<? extends IndexEntry> indexEntryClass : getIndexEntryClasses()) { 048 Collection<? extends IndexEntry> indexEntries = getAll(pm, indexEntryClass); 049 for (IndexEntry indexEntry : indexEntries) { 050 indexEntry.setKeyStoreRefID(commandApplyParam.getCryptoContext().getKeyStoreRefID()); 051 } 052 } 053 } 054 055 protected <T> Collection<T> getAll(PersistenceManager pm, Class<T> entityClass) { 056 Query q = pm.newQuery(entityClass); 057 @SuppressWarnings("unchecked") 058 Collection<T> c = (Collection<T>) q.execute(); 059 return c; 060 } 061 062 protected Set<Class<? extends IndexEntry>> getIndexEntryClasses() { 063 if (indexEntryClasses == null) 064 indexEntryClasses = ((Cumulus4jStoreManager)cryptoContext.getExecutionContext().getStoreManager()).getIndexFactoryRegistry().getIndexEntryClasses(); 065 066 return indexEntryClasses; 067 } 068 }