Coverage Report - org.jtheque.primary.services.impl.CollectionsService
 
Classes in this File Line Coverage Branch Coverage Complexity
CollectionsService
0 %
0/36
0 %
0/10
1.889
 
 1  
 package org.jtheque.primary.services.impl;
 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.language.ILanguageManager;
 21  
 import org.jtheque.core.managers.persistence.able.DataListener;
 22  
 import org.jtheque.primary.PrimaryUtils;
 23  
 import org.jtheque.primary.dao.able.IDaoCollections;
 24  
 import org.jtheque.primary.od.able.Collection;
 25  
 import org.jtheque.primary.services.able.ICollectionsService;
 26  
 import org.jtheque.utils.StringUtils;
 27  
 import org.jtheque.utils.io.FileUtils;
 28  
 import org.springframework.transaction.annotation.Transactional;
 29  
 
 30  
 import javax.annotation.Resource;
 31  
 
 32  
 /**
 33  
  * The implementation of the collections service.
 34  
  *
 35  
  * @author Baptiste Wicht
 36  
  */
 37  0
 public final class CollectionsService implements ICollectionsService {
 38  
         @Resource
 39  
         private IDaoCollections daoCollections;
 40  
 
 41  
         /**
 42  
          * Return an empty collection.
 43  
          *
 44  
          * @return An empty collection.
 45  
          */
 46  
         private Collection getEmptyCollection(){
 47  0
                 Collection emptyCollection = daoCollections.createCollection();
 48  
 
 49  0
                 emptyCollection.setTitle(Managers.getManager(ILanguageManager.class).getMessage("generic.view.actions.new"));
 50  0
                 emptyCollection.setProtection(false);
 51  0
                 emptyCollection.setPassword("");
 52  
 
 53  0
                 return emptyCollection;
 54  
         }
 55  
 
 56  
         /**
 57  
          * Indicate if a login is correct or not
 58  
          *
 59  
          * @param title The title of the collection.
 60  
          * @param password The password to login to the collection.
 61  
          *
 62  
          * @return true if the login is correct else false.
 63  
          */
 64  
         private boolean isLoginIncorrect(String title, String password){
 65  0
                 Collection collection = daoCollections.getCollection(title);
 66  
 
 67  0
                 if (collection == null){
 68  0
                         return true;
 69  
                 }
 70  
 
 71  0
                 if (collection.isProtection()){
 72  0
                         String encrypted = FileUtils.encryptKey(password);
 73  
 
 74  0
                         if (!encrypted.equals(collection.getPassword())){
 75  0
                                 return true;
 76  
                         }
 77  
                 }
 78  
 
 79  0
                 return false;
 80  
         }
 81  
 
 82  
         /**
 83  
          * Create a collection.
 84  
          *
 85  
          * @param title The title of the collection.
 86  
          * @param password The password of the collection.
 87  
          */
 88  
         @Transactional
 89  
         private void createCollection(String title, String password){
 90  0
                 Collection collection = getEmptyCollection();
 91  
 
 92  0
                 collection.setPrimaryImpl(PrimaryUtils.getPrimaryImpl());
 93  0
                 collection.setTitle(title);
 94  
 
 95  0
                 if (StringUtils.isEmpty(password)){
 96  0
                         collection.setProtection(false);
 97  
                 } else {
 98  0
                         collection.setProtection(true);
 99  0
                         collection.setPassword(FileUtils.encryptKey(password));
 100  
                 }
 101  
 
 102  0
                 daoCollections.create(collection);
 103  0
         }
 104  
 
 105  
         @Override
 106  
         public void createCollectionAndUse(String title, String password){
 107  0
                 createCollection(title, password);
 108  0
                 daoCollections.setCurrentCollection(daoCollections.getCollection(title));
 109  0
         }
 110  
 
 111  
         @Override
 112  
         public boolean login(String title, String password){
 113  0
                 if (isLoginIncorrect(title, password)){
 114  0
                         return false;
 115  
                 }
 116  
 
 117  0
                 daoCollections.setCurrentCollection(daoCollections.getCollection(title));
 118  
 
 119  0
                 return true;
 120  
         }
 121  
 
 122  
         @Override
 123  
         public java.util.Collection<Collection> getDatas(){
 124  0
                 return daoCollections.getCollections();
 125  
         }
 126  
 
 127  
         @Override
 128  
         public void addDataListener(DataListener listener){
 129  0
                 daoCollections.addDataListener(listener);
 130  0
         }
 131  
 
 132  
         @Override
 133  
         @Transactional
 134  
         public void clearAll(){
 135  0
                 daoCollections.clearAll();
 136  0
         }
 137  
 
 138  
         @Override
 139  
         public String getDataType(){
 140  0
                 return DATA_TYPE;
 141  
         }
 142  
 }