001    package org.cumulus4j.store.datastoreversion.command;
002    
003    import java.util.ArrayList;
004    import java.util.Collection;
005    
006    import javax.jdo.PersistenceManager;
007    import javax.jdo.Query;
008    
009    import org.cumulus4j.store.crypto.CryptoContext;
010    import org.cumulus4j.store.datastoreversion.AbstractDatastoreVersionCommand;
011    import org.cumulus4j.store.datastoreversion.CommandApplyParam;
012    import org.cumulus4j.store.model.Sequence;
013    import org.cumulus4j.store.model.Sequence2;
014    import org.cumulus4j.store.model.Sequence2DAO;
015    
016    @SuppressWarnings("deprecation")
017    public class MigrateToSequence2 extends AbstractDatastoreVersionCommand
018    {
019            @Override
020            public int getCommandVersion() {
021                    return 0;
022            }
023    
024            @Override
025            public void apply(CommandApplyParam commandApplyParam) {
026                    // The Sequence[2] only exists in the data-datastore (not in the index-datastore), hence we return immediately, if the
027                    // current datastore is not the data-datastore.
028                    PersistenceManager pm = commandApplyParam.getPersistenceManager();
029                    CryptoContext cryptoContext = commandApplyParam.getCryptoContext();
030                    if (pm != cryptoContext.getPersistenceManagerForData())
031                            return;
032    
033                    Sequence2DAO sequence2DAO = new Sequence2DAO(pm, cryptoContext.getKeyStoreRefID());
034    
035                    Query q = pm.newQuery(Sequence.class);
036                    @SuppressWarnings("unchecked")
037                    Collection<Sequence> c = (Collection<Sequence>) q.execute();
038                    for (Sequence sequence : new ArrayList<Sequence>(c)) { // should work without the 'new ArrayList...', but I can't test and it doesn't harm
039                            Sequence2 sequence2 = sequence2DAO.createSequence2(sequence.getSequenceName());
040                            sequence2.setNextValue(sequence.getNextValue());
041                            pm.deletePersistent(sequence);
042                    }
043            }
044    }