[6821] | 1 | package fr.mael.jiwigo.dao; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.util.ArrayList; |
---|
| 5 | import java.util.List; |
---|
| 6 | |
---|
| 7 | import org.jdom.Document; |
---|
| 8 | import org.jdom.Element; |
---|
| 9 | |
---|
| 10 | import fr.mael.jiwigo.Main; |
---|
| 11 | import fr.mael.jiwigo.om.Category; |
---|
| 12 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
| 13 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | Copyright (c) 2010, Mael |
---|
| 17 | All rights reserved. |
---|
| 18 | |
---|
| 19 | Redistribution and use in source and binary forms, with or without |
---|
| 20 | modification, are permitted provided that the following conditions are met: |
---|
| 21 | * Redistributions of source code must retain the above copyright |
---|
| 22 | notice, this list of conditions and the following disclaimer. |
---|
| 23 | * Redistributions in binary form must reproduce the above copyright |
---|
| 24 | notice, this list of conditions and the following disclaimer in the |
---|
| 25 | documentation and/or other materials provided with the distribution. |
---|
| 26 | * Neither the name of jiwigo nor the |
---|
| 27 | names of its contributors may be used to endorse or promote products |
---|
| 28 | derived from this software without specific prior written permission. |
---|
| 29 | |
---|
| 30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
| 31 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
| 32 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 33 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
| 34 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
| 35 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
| 36 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
| 37 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
| 38 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
| 39 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
| 40 | |
---|
| 41 | * Dao des catégories |
---|
| 42 | * @author mael |
---|
| 43 | * |
---|
| 44 | */ |
---|
| 45 | public class CategoryDao { |
---|
| 46 | /** |
---|
| 47 | * Logger |
---|
| 48 | */ |
---|
| 49 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(Main.class); |
---|
| 50 | /** |
---|
| 51 | * Instance, afin d'utiliser un singleton |
---|
| 52 | */ |
---|
| 53 | private static CategoryDao instance; |
---|
| 54 | /** |
---|
| 55 | * Cache. non utilisé pour le moment |
---|
| 56 | */ |
---|
| 57 | private List<Category> cache; |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Constructeur privé, afin que seule l'instance soit accessible |
---|
| 61 | */ |
---|
| 62 | private CategoryDao() { |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * @return l'instance |
---|
| 68 | */ |
---|
| 69 | public static CategoryDao getInstance() { |
---|
| 70 | if (instance == null) { |
---|
| 71 | instance = new CategoryDao(); |
---|
| 72 | } |
---|
| 73 | return instance; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Lister les catégorie |
---|
| 78 | * @param rafraichir permet de dire si on utilise les catégories présentes dans le cache ou pas |
---|
| 79 | * @param recursive listing récursif des catégories |
---|
| 80 | * @return la liste des catégories |
---|
| 81 | * @throws IOException |
---|
| 82 | */ |
---|
| 83 | public List<Category> lister(boolean rafraichir, boolean recursive) throws IOException { |
---|
| 84 | if (cache == null || rafraichir) { |
---|
| 85 | Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), |
---|
| 86 | "recursive", String.valueOf(recursive)); |
---|
| 87 | Element element = doc.getRootElement().getChild("categories"); |
---|
| 88 | List<Element> listElement = (List<Element>) element.getChildren("category"); |
---|
| 89 | ArrayList<Category> categories = new ArrayList<Category>(); |
---|
| 90 | for (Element cat : listElement) { |
---|
| 91 | Category myCat = new Category(cat); |
---|
| 92 | categories.add(myCat); |
---|
| 93 | } |
---|
| 94 | this.cache = categories; |
---|
| 95 | } |
---|
| 96 | return cache; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Création d'une catégorie |
---|
| 101 | * @param category la catégorie à créer |
---|
| 102 | * @return true si la catégorie a bien été créée |
---|
| 103 | */ |
---|
| 104 | public boolean creer(Category category) { |
---|
| 105 | try { |
---|
| 106 | return Outil.checkOk(Main.sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), |
---|
| 107 | "name", category.getNom(), "parent", String.valueOf(category.getParentDirect()))); |
---|
| 108 | } catch (IOException e) { |
---|
| 109 | LOG.error(Outil.getStackTrace(e)); |
---|
| 110 | } |
---|
| 111 | return false; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | } |
---|