source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/service/CategoryService.java @ 9387

Last change on this file since 9387 was 9387, checked in by mlg, 13 years ago

First commit for jiwigo-ws-api

File size: 4.0 KB
Line 
1package fr.mael.jiwigo.service;
2
3import java.io.IOException;
4import java.util.List;
5
6import fr.mael.jiwigo.dao.CategoryDao;
7import fr.mael.jiwigo.om.Category;
8import fr.mael.jiwigo.transverse.session.SessionManager;
9
10/*
11Copyright (c) 2010, Mael
12All rights reserved.
13
14Redistribution and use in source and binary forms, with or without
15modification, are permitted provided that the following conditions are met:
16 * Redistributions of source code must retain the above copyright
17   notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above copyright
19   notice, this list of conditions and the following disclaimer in the
20   documentation and/or other materials provided with the distribution.
21 * Neither the name of jiwigo nor the
22   names of its contributors may be used to endorse or promote products
23   derived from this software without specific prior written permission.
24
25THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
29DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*/
36/**
37
38 * @author mael
39 *
40 */
41public class CategoryService {
42    /**
43     * Logger
44     */
45    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
46            .getLog(CategoryService.class);
47
48    /**
49     * singleton
50     */
51    private static CategoryService instance;
52
53    private SessionManager sessionManager;
54
55    /**
56     * @return the singleton
57     */
58    public static CategoryService getInstance(SessionManager sessionManager) {
59        if (instance == null) {
60            instance = new CategoryService(sessionManager);
61        }
62        return instance;
63    }
64
65    /**
66     * private constructor to use a singleton
67     */
68    private CategoryService(SessionManager sessionManager) {
69        this.sessionManager = sessionManager;
70
71    }
72
73    /**
74     * Lists all categories
75     * @param rafraichir true to refresh
76     * @param recursive true : recursive listing
77     * @return the list of categories
78     * @throws IOException
79     */
80    public List<Category> lister(boolean recursive) throws IOException {
81        return CategoryDao.getInstance(sessionManager).lister(recursive);
82    }
83
84    /**
85     * Allows to create the categories tree
86     * @return list of categories
87     * @throws IOException
88     */
89    public List<Category> makeTree() throws IOException {
90        List<Category> list = CategoryDao.getInstance(sessionManager).lister(true);
91        for (Category category : list) {
92            for (Category category2 : list) {
93                if (category2.getIdCategoriesMeres().size() != 1
94                        && category.getIdentifiant().equals(
95                                category2.getIdCategoriesMeres().get(category2.getIdCategoriesMeres().size() - 2))) {
96                    category.getCategoriesFilles().add(category2);
97                    category2.getCategoriesMeres().add(category);
98                }
99
100            }
101        }
102
103        return list;
104
105    }
106
107    /**
108     * creation of a category
109     * @param nom name of the category
110     * @param parent parent category
111     * @return true if successful
112     */
113    public boolean creer(String nom, Integer parent) {
114        Category category = new Category();
115        category.setParentDirect(parent);
116        category.setNom(nom);
117        return CategoryDao.getInstance(sessionManager).creer(category);
118    }
119
120    /**
121     * creation of a category without parent
122     * @param nom name of the category
123     * @return true if successful
124     */
125    public boolean creer(String nom) {
126        Category category = new Category();
127        category.setNom(nom);
128        return CategoryDao.getInstance(sessionManager).creer(category);
129    }
130}
Note: See TracBrowser for help on using the repository browser.