Ignore:
Timestamp:
Mar 29, 2011, 8:21:25 PM (13 years ago)
Author:
mlg
Message:

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CategoryDao.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.List;
    65
    7 import org.w3c.dom.Document;
    8 import org.w3c.dom.Element;
    9 import org.w3c.dom.Node;
    10 import org.w3c.dom.NodeList;
     6import fr.mael.jiwigo.om.Category;
     7import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    118
    12 import fr.mael.jiwigo.om.Category;
    13 import fr.mael.jiwigo.transverse.enumeration.CategoryEnum;
    14 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    15 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    16 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    17 import fr.mael.jiwigo.transverse.session.SessionManager;
    18 import fr.mael.jiwigo.transverse.util.Tools;
    19 
    20 /*
    21  *  jiwigo-ws-api Piwigo webservice access Api
    22  *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
    23  *                All Rights Reserved
    24  *
    25  *  This library is free software. It comes without any warranty, to
    26  *  the extent permitted by applicable law. You can redistribute it
    27  *  and/or modify it under the terms of the Do What The Fuck You Want
    28  *  To Public License, Version 2, as published by Sam Hocevar. See
    29  *  http://sam.zoy.org/wtfpl/COPYING for more details.
    30  */
    31 /**
    32  * Dao for the categories
    33  * @author mael
    34  *
    35  */
    36 public class CategoryDao {
    37     /**
    38      * Logger
    39      */
    40     public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
    41             .getLog(CategoryDao.class);
    42     /**
    43      * Instance to use a singleton
    44      */
    45     private static CategoryDao instance;
    46 
    47     private SessionManager sessionManager;
    48 
    49     /**
    50      * Private constructor to use a singleton
    51      */
    52     private CategoryDao(SessionManager sessionManager) {
    53         this.sessionManager = sessionManager;
    54     }
    55 
    56     /**
    57      * @return the instance
    58      */
    59     public static CategoryDao getInstance(SessionManager sessionManager) {
    60         if (instance == null) {
    61             instance = new CategoryDao(sessionManager);
    62         }
    63         return instance;
    64     }
     9public interface CategoryDao {
    6510
    6611    /**
     
    7116     * @throws ProxyAuthenticationException
    7217     */
    73     public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException {
    74         Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive",
    75                 String.valueOf(recursive));
    76         Element element = (Element) doc.getDocumentElement().getElementsByTagName("categories").item(0);
    77         NodeList nodeList = element.getElementsByTagName("category");
    78         ArrayList<Category> categories = new ArrayList<Category>();
    79         for (int i = 0; i < nodeList.getLength(); i++) {
    80             Node catNode = nodeList.item(i);
    81             if (catNode.getNodeType() == Node.ELEMENT_NODE) {
    82                 Element cat = (Element) catNode;
    83                 Category myCat = new Category();
    84                 myCat.setIdentifier(Integer.valueOf(cat.getAttribute(CategoryEnum.ID.getLabel())));
    85                 myCat.setUrlCategory(cat.getAttribute(CategoryEnum.URL.getLabel()));
    86                 myCat.setNbImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_IMAGES.getLabel())));
    87                 myCat.setNbTotalImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_TOTAL_IMAGES.getLabel())));
    88                 myCat.setName(Tools.getStringValueDom(cat, CategoryEnum.NAME.getLabel()));
    89                 String catMeres = Tools.getStringValueDom(cat, CategoryEnum.CAT_MERES.getLabel());
    90                 ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>();
    91                 myCat.setIdParentCategoriesString(catMeres);
    92                 for (String catA : catMeres.split(",")) {
    93                     if (!catA.equals("")) {
    94                         idCategoriesMeres.add(Integer.valueOf(catA));
    95                     }
    96                 }
    97                 myCat.setIdParentCategories(idCategoriesMeres);
    98                 categories.add(myCat);
    99             }
    100         }
    101         return categories;
    102     }
     18    public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException;
    10319
    10420    /**
     
    10824     * @throws ProxyAuthenticationException
    10925     */
    110     public boolean create(Category category) throws ProxyAuthenticationException {
    111         try {
    112             if (category.getDirectParent() != null) {
    113                 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
    114                         "name", category.getName(), "parent", String.valueOf(category.getDirectParent())));
    115             } else {
    116                 return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
    117                         "name", category.getName()));
    118             }
    119         } catch (IOException e) {
    120             LOG.error(Tools.getStackTrace(e));
    121         } catch (FileAlreadyExistsException e) {
    122             LOG.error(Tools.getStackTrace(e));
    123         }
    124         return false;
    125     }
    126 
     26    public boolean create(Category category) throws ProxyAuthenticationException;
    12727}
Note: See TracChangeset for help on using the changeset viewer.