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

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

Changes function name (to english)
version changed to 0.13.1

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 recursive true : recursive listing
76     * @return the list of categories
77     * @throws IOException
78     */
79    public List<Category> list(boolean recursive) throws IOException {
80        return CategoryDao.getInstance(sessionManager).list(recursive);
81    }
82
83    /**
84     * Allows to create the categories tree
85     * @return list of categories
86     * @throws IOException
87     */
88    public List<Category> makeTree() throws IOException {
89        List<Category> list = CategoryDao.getInstance(sessionManager).list(true);
90        for (Category category : list) {
91            for (Category category2 : list) {
92                if (category2.getIdParentCategories().size() != 1
93                        && category.getIdentifier().equals(
94                                category2.getIdParentCategories().get(category2.getIdParentCategories().size() - 2))) {
95                    category.getChildCategories().add(category2);
96                    category2.getParentCategories().add(category);
97                }
98
99            }
100        }
101
102        return list;
103
104    }
105
106    /**
107     * creation of a category
108     * @param nom name of the category
109     * @param parent parent category
110     * @return true if successful
111     */
112    public boolean create(String nom, Integer parent) {
113        Category category = new Category();
114        category.setDirectParent(parent);
115        category.setName(nom);
116        return CategoryDao.getInstance(sessionManager).create(category);
117    }
118
119    /**
120     * creation of a category without parent
121     * @param nom name of the category
122     * @return true if successful
123     */
124    public boolean create(String nom) {
125        Category category = new Category();
126        category.setName(nom);
127        return CategoryDao.getInstance(sessionManager).create(category);
128    }
129}
Note: See TracBrowser for help on using the repository browser.