BrightSide Workbench Full Report + Source Code
CategoryFilter.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.publication.www;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.string.Strings;
26 import org.turro.auth.Authentication;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.impl.navigation.DefaultNavigatorItem;
30 import org.turro.html.HTMLHelper;
31 import org.turro.i18n.I_;
32 import org.turro.plugin.contacts.IContact;
33 import org.turro.publication.db.PublicationPU;
34 import org.turro.publication.entity.PublicationCategory;
35 import org.turro.publication.util.PublicationCategories;
36 import org.turro.util.Converter;
37 
42 public class CategoryFilter {
43 
44  private final IConstructor constructor;
45  private final String uniqueId;
46 
47  private HTMLHelper html;
48  private boolean blogger;
49 
50  public CategoryFilter(IConstructor constructor, String uniqueId) {
51  this.constructor = constructor;
52  this.uniqueId = uniqueId;
53  }
54 
55  public boolean isBlogger() {
56  return blogger;
57  }
58 
59  public void setBlogger(boolean blogger) {
60  this.blogger = blogger;
61  }
62 
63  public Collection<PublicationCategory> getCategories(long groupId) {
64  ArrayList<PublicationCategory> list = new ArrayList<>();
66  list.addAll(PublicationCategories.getPublicCategories(contact));
67  if(blogger) {
69  }
70  return list;
71  }
72 
73  public Collection<DefaultNavigatorItem> pubCategories(String parameters, long groupId) {
74  PublicationCategory selected = getCategory(constructor, uniqueId);
75  ArrayList<DefaultNavigatorItem> cats = new ArrayList<>();
76 
77  cats.add(new DefaultNavigatorItem(I_.get("All categories"), parameters + "&category=0", null, selected == null));
78  for(PublicationCategory pc : getCategories(groupId)) {
79  cats.add(new DefaultNavigatorItem(pc.getName(), parameters + "&category=" + pc.getId(), null, selected != null && pc.getId() == selected.getId()));
80  }
81  return cats;
82  }
83 
84  public void renderCategories(String parameters, long groupId) {
85  PublicationCategory selected = getCategory(constructor, uniqueId);
86  html = new HTMLHelper(constructor);
87  html.startTag("div", "class='publication-categories'");
88  if(selected == null) {
89  html.setExtraAttributes("class='publication-category-selected'");
90  } else {
91  html.setExtraAttributes("class='publication-category'");
92  }
93  html.startAnchor(parameters + "&category=0", null)
94  .write(I_.get("All categories"))
95  .endTag();
96  for(PublicationCategory pc : getCategories(groupId)) {
97  if(selected != null && pc.getId() == selected.getId()) {
98  html.setExtraAttributes("class='publication-category-selected'");
99  } else {
100  html.setExtraAttributes("class='publication-category'");
101  }
102  html.startAnchor(parameters + "&category=" + pc.getId(), null)
103  .write(pc.getName())
104  .endTag();
105  }
106  html.endAllTags();
107  }
108 
109  public void processParameters() {
110  String category = constructor.getParameter("category");
111  try {
112  if(!Strings.isBlank(category)) {
113  long id = Converter.STANDARD.convert(category, Long.class, 0L);
114  if(id == 0) {
115  constructor.removeSessionAttribute("/publication-category/" + uniqueId);
116  } else {
117  constructor.setSessionAttribute("/publication-category/" + uniqueId,
118  new PublicationPU().find(PublicationCategory.class, id));
119  }
120  }
121  } catch(Exception ex) {
122  Logger.getLogger(CategoryFilter.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
123  }
124  }
125 
126  public static PublicationCategory getCategory(IConstructor constructor, String uniqueId) {
127  return uniqueId == null ? null : (PublicationCategory) constructor.getSessionAttribute("/publication-category/" + uniqueId);
128  }
129 
130 }
HTMLGenerator write(String value)
void setExtraAttributes(String extraAttributes)
HTMLGenerator startTag(String tag)
HTMLGenerator startAnchor(String url, String hint)
static String get(String msg)
Definition: I_.java:41
static Collection< PublicationCategory > getPublicCategories()
static Collection<? extends PublicationCategory > getPrivateBloggerCategories(IContact contact)
static PublicationCategory getCategory(IConstructor constructor, String uniqueId)
Collection< DefaultNavigatorItem > pubCategories(String parameters, long groupId)
Collection< PublicationCategory > getCategories(long groupId)
void renderCategories(String parameters, long groupId)
CategoryFilter(IConstructor constructor, String uniqueId)
void setSessionAttribute(String key, Object value)