Android Open Source - notes Storage Data Transfer






From Project

Back to project page notes.

License

The source code is released under:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute verbatim or...

If you think the Android project notes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iliakplv.notes.notes.storage;
//from  w ww .j a v a 2  s .  c o  m

import android.util.Pair;

import com.iliakplv.notes.analytics.EventTracker;
import com.iliakplv.notes.notes.AbstractNote;
import com.iliakplv.notes.notes.Label;
import com.iliakplv.notes.utils.AppLog;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public final class StorageDataTransfer {

  private static final String TAG = StorageDataTransfer.class.getSimpleName();

  private static final NotesStorage storage = Storage.getStorage();


  private static List<AbstractNote> notesBackup;
  private static List<Label> labelsBackup;
  private static Set<Pair<Serializable, Serializable>> notesLabelsBackup;
  private static boolean backupPerformed = false;


  private static void backupCurrentStorage() {
    notesBackup = storage.getNotesForLabel(NotesStorage.NOTES_FOR_ALL_LABELS);
    labelsBackup = storage.getAllLabels();
    notesLabelsBackup = storage.getAllNotesLabelsIds();
    backupPerformed = true;
  }

  private static void restoreBackup() {
    checkBackupPerformed();

    // Ids mapping
    final HashMap<Serializable, Serializable> notesOldToNewIdsMap =
        new HashMap<Serializable, Serializable>();
    final HashMap<Serializable, Serializable> labelsOldToNewIdsMap =
        new HashMap<Serializable, Serializable>();

    // restore notes
    for (AbstractNote note : notesBackup) {
      Serializable newId = storage.insertNote(note);
      notesOldToNewIdsMap.put(note.getId(), newId);
    }

    // restore labels
    for (Label label : labelsBackup) {
      Serializable newId = storage.insertLabel(label);
      labelsOldToNewIdsMap.put(label.getId(), newId);
    }

    // restore notes_labels
    for (Pair<Serializable, Serializable> noteIdLabelId : notesLabelsBackup) {
      Serializable newNoteId = notesOldToNewIdsMap.get(noteIdLabelId.first);
      Serializable newLabelId = labelsOldToNewIdsMap.get(noteIdLabelId.second);
      storage.insertLabelToNote(newNoteId, newLabelId);
    }
  }

  private static void checkBackupPerformed() {
    if (!backupPerformed) {
      throw new IllegalStateException("Backup not performed!");
    }
  }

  private static void clearCurrentStorage() {
    checkBackupPerformed();
    storage.clear();
  }

  private static void clearBackup() {
    notesBackup = null;
    labelsBackup = null;
    notesLabelsBackup = null;
    backupPerformed = false;
  }


  public static synchronized void changeStorageType(Storage.Type newStorageType, boolean clearCurrentStorage) {
    if (newStorageType == null) {
      throw new NullPointerException("New storage type is null");
    }
    if (Storage.getCurrentStorageType() == newStorageType) {
      return;
    }

    // event tracking state backup
    final boolean eventTrackingWasEnabled = EventTracker.isEnabled();
    EventTracker.setEnabled(false);
    // listeners backup
    final List<NotesStorageListener> listeners = Storage.getStorage().detachAllListeners();

// data transfer start

    backupCurrentStorage();
    if (clearCurrentStorage) {
      clearCurrentStorage();
    }

    boolean newStorageInitialized = false;
    try {
      Storage.init(newStorageType);
      newStorageInitialized = true;
    } catch (Exception e) {
      AppLog.e(TAG, "Exception during storage initialization", e);
    }

    // restore data to new initialized storage or
    // to old storage if new storage has not been initialized and old has been cleared
    if (newStorageInitialized || clearCurrentStorage) {
      restoreBackup();
    }

    clearBackup();

// data transfer end

    // listeners restore
    Storage.getStorage().attachListeners(listeners);
    // event tracking state restore
    EventTracker.setEnabled(eventTrackingWasEnabled);
  }
}




Java Source Code List

com.iliakplv.notes.NotesApplication.java
com.iliakplv.notes.analytics.EventTracker.java
com.iliakplv.notes.analytics.Event.java
com.iliakplv.notes.gui.main.MainActivityTest.java
com.iliakplv.notes.gui.main.MainActivity.java
com.iliakplv.notes.gui.main.NavigationDrawerFragment.java
com.iliakplv.notes.gui.main.NoteDetailsFragment.java
com.iliakplv.notes.gui.main.NotesListFragment.java
com.iliakplv.notes.gui.main.dialogs.AboutDialog.java
com.iliakplv.notes.gui.main.dialogs.AbstractItemDialog.java
com.iliakplv.notes.gui.main.dialogs.DropboxAccountLinkingDialog.java
com.iliakplv.notes.gui.main.dialogs.LabelEditDialog.java
com.iliakplv.notes.gui.main.dialogs.NoteLabelsDialog.java
com.iliakplv.notes.gui.main.dialogs.SimpleItemDialog.java
com.iliakplv.notes.gui.main.dialogs.VoiceSearchInstallDialog.java
com.iliakplv.notes.gui.settings.SettingsActivity.java
com.iliakplv.notes.notes.AbstractNote.java
com.iliakplv.notes.notes.LabelComparator.java
com.iliakplv.notes.notes.Label.java
com.iliakplv.notes.notes.NoteComparator.java
com.iliakplv.notes.notes.NotesUtils.java
com.iliakplv.notes.notes.TextNote.java
com.iliakplv.notes.notes.db.NotesDatabaseAdapter.java
com.iliakplv.notes.notes.db.NotesDatabaseOpenHelper.java
com.iliakplv.notes.notes.db.NotesDatabaseStorage.java
com.iliakplv.notes.notes.dropbox.DropboxHelper.java
com.iliakplv.notes.notes.dropbox.NotesDropboxStorage.java
com.iliakplv.notes.notes.storage.NotesStorageListener.java
com.iliakplv.notes.notes.storage.NotesStorage.java
com.iliakplv.notes.notes.storage.StorageDataTransfer.java
com.iliakplv.notes.notes.storage.StorageWrapper.java
com.iliakplv.notes.notes.storage.Storage.java
com.iliakplv.notes.storage.StorageTest.java
com.iliakplv.notes.utils.AppLog.java
com.iliakplv.notes.utils.ConnectivityUtils.java
com.iliakplv.notes.utils.StringUtils.java
com.iliakplv.notes.utils.Utils.java