[9919] | 1 | package fr.mael.jiwigo.dao.impl; |
---|
[9387] | 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.util.ArrayList; |
---|
| 5 | import java.util.List; |
---|
| 6 | |
---|
[9919] | 7 | import org.slf4j.Logger; |
---|
| 8 | import org.slf4j.LoggerFactory; |
---|
[9902] | 9 | import org.w3c.dom.Document; |
---|
| 10 | import org.w3c.dom.Element; |
---|
| 11 | import org.w3c.dom.Node; |
---|
| 12 | import org.w3c.dom.NodeList; |
---|
[9387] | 13 | |
---|
[9919] | 14 | import fr.mael.jiwigo.dao.CategoryDao; |
---|
[9387] | 15 | import fr.mael.jiwigo.om.Category; |
---|
| 16 | import fr.mael.jiwigo.transverse.enumeration.CategoryEnum; |
---|
| 17 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
[9893] | 18 | import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; |
---|
[10505] | 19 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
[9430] | 20 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
[9387] | 21 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
[9392] | 22 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
[9387] | 23 | |
---|
| 24 | /* |
---|
[9879] | 25 | * jiwigo-ws-api Piwigo webservice access Api |
---|
| 26 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
| 27 | * All Rights Reserved |
---|
| 28 | * |
---|
| 29 | * This library is free software. It comes without any warranty, to |
---|
| 30 | * the extent permitted by applicable law. You can redistribute it |
---|
| 31 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 32 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
| 33 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 34 | */ |
---|
[9387] | 35 | /** |
---|
| 36 | * Dao for the categories |
---|
| 37 | * @author mael |
---|
| 38 | * |
---|
| 39 | */ |
---|
[9919] | 40 | public class CategoryDaoImpl implements CategoryDao { |
---|
[9387] | 41 | /** |
---|
| 42 | * Logger |
---|
| 43 | */ |
---|
[9919] | 44 | private final Logger LOG = LoggerFactory.getLogger(CategoryDaoImpl.class); |
---|
[9387] | 45 | /** |
---|
| 46 | * Instance to use a singleton |
---|
| 47 | */ |
---|
[9919] | 48 | private static CategoryDaoImpl instance; |
---|
[9387] | 49 | |
---|
| 50 | private SessionManager sessionManager; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Lists the categories |
---|
| 54 | * @param recursive true : recursive listing of the categories |
---|
| 55 | * @return the list of categories |
---|
[10505] | 56 | * @throws JiwigoException |
---|
[9387] | 57 | * @throws IOException |
---|
[9430] | 58 | * @throws ProxyAuthenticationException |
---|
[9387] | 59 | */ |
---|
[10505] | 60 | public List<Category> list(boolean recursive) throws JiwigoException { |
---|
[9392] | 61 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive", |
---|
[9387] | 62 | String.valueOf(recursive)); |
---|
[9902] | 63 | Element element = (Element) doc.getDocumentElement().getElementsByTagName("categories").item(0); |
---|
| 64 | NodeList nodeList = element.getElementsByTagName("category"); |
---|
[9387] | 65 | ArrayList<Category> categories = new ArrayList<Category>(); |
---|
[9902] | 66 | for (int i = 0; i < nodeList.getLength(); i++) { |
---|
| 67 | Node catNode = nodeList.item(i); |
---|
| 68 | if (catNode.getNodeType() == Node.ELEMENT_NODE) { |
---|
| 69 | Element cat = (Element) catNode; |
---|
| 70 | Category myCat = new Category(); |
---|
| 71 | myCat.setIdentifier(Integer.valueOf(cat.getAttribute(CategoryEnum.ID.getLabel()))); |
---|
| 72 | myCat.setUrlCategory(cat.getAttribute(CategoryEnum.URL.getLabel())); |
---|
| 73 | myCat.setNbImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_IMAGES.getLabel()))); |
---|
| 74 | myCat.setNbTotalImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_TOTAL_IMAGES.getLabel()))); |
---|
| 75 | myCat.setName(Tools.getStringValueDom(cat, CategoryEnum.NAME.getLabel())); |
---|
| 76 | String catMeres = Tools.getStringValueDom(cat, CategoryEnum.CAT_MERES.getLabel()); |
---|
| 77 | ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>(); |
---|
| 78 | myCat.setIdParentCategoriesString(catMeres); |
---|
| 79 | for (String catA : catMeres.split(",")) { |
---|
| 80 | if (!catA.equals("")) { |
---|
| 81 | idCategoriesMeres.add(Integer.valueOf(catA)); |
---|
| 82 | } |
---|
[9387] | 83 | } |
---|
[9902] | 84 | myCat.setIdParentCategories(idCategoriesMeres); |
---|
| 85 | categories.add(myCat); |
---|
[9387] | 86 | } |
---|
| 87 | } |
---|
| 88 | return categories; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Creation of a category |
---|
| 93 | * @param category the category to create |
---|
| 94 | * @return true if the category is successfully created |
---|
[9430] | 95 | * @throws ProxyAuthenticationException |
---|
[10505] | 96 | * @throws JiwigoException |
---|
[9387] | 97 | */ |
---|
[10505] | 98 | public boolean create(Category category) throws JiwigoException { |
---|
[9387] | 99 | try { |
---|
[9392] | 100 | if (category.getDirectParent() != null) { |
---|
| 101 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), |
---|
| 102 | "name", category.getName(), "parent", String.valueOf(category.getDirectParent()))); |
---|
[9387] | 103 | } else { |
---|
[9392] | 104 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), |
---|
| 105 | "name", category.getName())); |
---|
[9387] | 106 | } |
---|
[9893] | 107 | } catch (FileAlreadyExistsException e) { |
---|
| 108 | LOG.error(Tools.getStackTrace(e)); |
---|
[10505] | 109 | throw new JiwigoException(e); |
---|
[9387] | 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
[10714] | 113 | /** |
---|
| 114 | * @see fr.mael.jiwigo.dao.CategoryDao#delete(fr.mael.jiwigo.om.Category) |
---|
| 115 | */ |
---|
| 116 | public boolean delete(Category category) throws JiwigoException { |
---|
| 117 | //this document will allow to get pwg_token. it is necessary to delete a category |
---|
| 118 | Document docStatus = sessionManager.executeReturnDocument(MethodsEnum.SESSION_STATUS.getLabel()); |
---|
| 119 | String pwgToken = Tools.getStringValueDom(docStatus.getDocumentElement(), "pwg_token"); |
---|
| 120 | if (pwgToken.equals("")) { |
---|
| 121 | throw new JiwigoException("Error getting pwg_token. Returned document was : " |
---|
| 122 | + Tools.documentToString(docStatus)); |
---|
| 123 | } |
---|
| 124 | if (LOG.isDebugEnabled()) { |
---|
| 125 | LOG.debug("Deletes category " + category.getIdentifier() + " with pwg_token = " + pwgToken); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.DELETE_CATEGORY.getLabel(), "category_id", |
---|
| 129 | String.valueOf(category.getIdentifier()), "pwg_token", pwgToken); |
---|
| 130 | |
---|
| 131 | if (LOG.isDebugEnabled()) { |
---|
| 132 | LOG.debug(Tools.documentToString(doc)); |
---|
| 133 | } |
---|
| 134 | try { |
---|
| 135 | return Tools.checkOk(doc); |
---|
| 136 | } catch (FileAlreadyExistsException e) { |
---|
| 137 | e.printStackTrace(); |
---|
| 138 | } |
---|
| 139 | return false; |
---|
| 140 | } |
---|
| 141 | |
---|
[9919] | 142 | public SessionManager getSessionManager() { |
---|
| 143 | return sessionManager; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | public void setSessionManager(SessionManager sessionManager) { |
---|
| 147 | this.sessionManager = sessionManager; |
---|
| 148 | } |
---|
| 149 | |
---|
[9387] | 150 | } |
---|