Coverage Report - org.jtheque.core.managers.view.impl.actions.backup.AcRestore
 
Classes in this File Line Coverage Branch Coverage Complexity
AcRestore
0 %
0/12
0 %
0/2
1.5
AcRestore$1
0 %
0/4
N/A
1.5
AcRestore$RestoreRunnable
0 %
0/14
0 %
0/2
1.5
AcRestore$RestoreRunnable$1
0 %
0/3
N/A
1.5
 
 1  
 package org.jtheque.core.managers.view.impl.actions.backup;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.error.IErrorManager;
 21  
 import org.jtheque.core.managers.file.IFileManager;
 22  
 import org.jtheque.core.managers.file.able.FileType;
 23  
 import org.jtheque.core.managers.log.IJThequeLogger;
 24  
 import org.jtheque.core.managers.log.Logger;
 25  
 import org.jtheque.core.managers.persistence.IPersistenceManager;
 26  
 import org.jtheque.core.managers.view.able.IMainView;
 27  
 import org.jtheque.core.managers.view.able.IViewManager;
 28  
 import org.jtheque.core.managers.view.edt.SimpleTask;
 29  
 import org.jtheque.core.managers.view.impl.actions.JThequeAction;
 30  
 import org.jtheque.utils.io.FileException;
 31  
 import org.jtheque.utils.io.SimpleFilter;
 32  
 
 33  
 import javax.annotation.Resource;
 34  
 import java.awt.event.ActionEvent;
 35  
 import java.io.File;
 36  
 
 37  
 /**
 38  
  * Action to restore from JTD.
 39  
  *
 40  
  * @author Baptiste Wicht
 41  
  */
 42  0
 public class AcRestore extends JThequeAction {
 43  
     private final SimpleFilter filter;
 44  
     private final FileType type;
 45  
 
 46  
     @Resource
 47  
     private IMainView mainView;
 48  
 
 49  
     @Resource
 50  
     private IFileManager fileManager;
 51  
 
 52  
     @Resource
 53  
     private IViewManager viewManager;
 54  
 
 55  
     @Logger
 56  
     private IJThequeLogger logger;
 57  
 
 58  
     /**
 59  
      * Construct a new AcRestoreFromJTD.
 60  
      *
 61  
      * @param key    The internationalization key.
 62  
      * @param filter The file filter.
 63  
      * @param type   The file type.
 64  
      */
 65  
     AcRestore(String key, SimpleFilter filter, FileType type) {
 66  0
         super(key);
 67  
 
 68  0
         this.filter = filter;
 69  0
         this.type = type;
 70  0
     }
 71  
 
 72  
     @Override
 73  
     public final void actionPerformed(ActionEvent arg0) {
 74  0
         if (fileManager.isRestorePossible(type)) {
 75  0
             final File file = new File(viewManager.chooseFile(filter));
 76  
 
 77  0
             final boolean yes = viewManager.askI18nUserForConfirmation(
 78  
                     "dialogs.confirm.clear.database", "dialogs.confirm.clear.database.title");
 79  
 
 80  0
             viewManager.execute(new SimpleTask() {
 81  
                 @Override
 82  
                 public void run() {
 83  0
                     mainView.startWait();
 84  
 
 85  0
                     new Thread(new RestoreRunnable(yes, file)).start();
 86  0
                 }
 87  
             });
 88  0
         } else {
 89  0
             viewManager.displayI18nText("error.restore.nothing");
 90  
         }
 91  0
     }
 92  
 
 93  
     /**
 94  
      * A runnable to launch the restore process.
 95  
      *
 96  
      * @author Baptiste Wicht
 97  
      */
 98  
     private final class RestoreRunnable implements Runnable {
 99  
         private final boolean clear;
 100  
         private final File file;
 101  
 
 102  
         /**
 103  
          * Create a new RestoreRunnable.
 104  
          *
 105  
          * @param clear indicate if we must clear the database or not.
 106  
          * @param file  The file to restore from.
 107  
          */
 108  0
         RestoreRunnable(boolean clear, File file) {
 109  0
             super();
 110  
 
 111  0
             this.clear = clear;
 112  0
             this.file = file;
 113  0
         }
 114  
 
 115  
         @Override
 116  
         public void run() {
 117  0
             if (clear) {
 118  0
                 Managers.getManager(IPersistenceManager.class).clearDatabase();
 119  
             }
 120  
 
 121  
             try {
 122  0
                 fileManager.restore(type, file);
 123  0
             } catch (FileException e) {
 124  0
                 logger.error(e);
 125  0
                 Managers.getManager(IErrorManager.class).addInternationalizedError("error.restore.error");
 126  0
             }
 127  
 
 128  0
             viewManager.execute(new SimpleTask() {
 129  
                 @Override
 130  
                 public void run() {
 131  0
                     mainView.stopWait();
 132  0
                 }
 133  
             });
 134  0
         }
 135  
     }
 136  
 }