001package co.codewizards.cloudstore.local.persistence;
002
003import static co.codewizards.cloudstore.core.objectfactory.ObjectFactoryUtil.*;
004import static java.util.Objects.*;
005
006import java.util.LinkedHashSet;
007
008import javax.jdo.FetchPlan;
009import javax.jdo.PersistenceManager;
010
011public class FetchPlanBackup {
012
013        private LinkedHashSet<String> groups;
014        private int fetchSize;
015        private int maxFetchDepth;
016
017        protected FetchPlanBackup() {
018        }
019
020        public static FetchPlanBackup createFrom(final PersistenceManager pm) {
021                return createFrom(requireNonNull(pm, "pm").getFetchPlan());
022        }
023
024        public static FetchPlanBackup createFrom(final FetchPlan fetchPlan) {
025                requireNonNull(fetchPlan, "fetchPlan");
026                final FetchPlanBackup fetchPlanBackup = createObject(FetchPlanBackup.class);
027                fetchPlanBackup.init(fetchPlan);
028                return fetchPlanBackup;
029        }
030
031        @SuppressWarnings("unchecked")
032        public void init(final FetchPlan fetchPlan) {
033                requireNonNull(fetchPlan, "fetchPlan");
034                this.groups = new LinkedHashSet<String>(fetchPlan.getGroups());
035                this.fetchSize = fetchPlan.getFetchSize();
036                this.maxFetchDepth = fetchPlan.getMaxFetchDepth();
037        }
038
039        public void restore(final PersistenceManager pm) {
040                restore(requireNonNull(pm, "pm").getFetchPlan());
041        }
042
043        public void restore(final FetchPlan fetchPlan) {
044                requireNonNull(fetchPlan, "fetchPlan");
045                if (groups == null)
046                        throw new IllegalStateException("init(...) was not called!");
047
048                fetchPlan.setGroups(groups);
049                fetchPlan.setFetchSize(fetchSize);
050                fetchPlan.setMaxFetchDepth(maxFetchDepth);
051        }
052}