Java tutorial
/* * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.scoppelletti.wui; import java.util.*; import com.opensymphony.xwork2.*; import org.restlet.resource.*; import org.slf4j.*; import org.springframework.web.util.*; import it.scoppelletti.programmerpower.reflect.*; import it.scoppelletti.programmerpower.types.*; import it.scoppelletti.programmerpower.web.control.*; import it.scoppelletti.programmerpower.web.rest.*; import it.scoppelletti.programmerpower.wui.*; /** * Pannello delle attività. */ @Final public class ActivityPanelAction extends ActionBase { private static final long serialVersionUID = 1L; /** * Identificatore della vista. Il valore della costante è * <CODE>{@value}</CODE>. */ public static final String VIEWID = "it-scoppelletti-wui-activityPanel"; private static final Logger myLogger = LoggerFactory.getLogger(ActivityPanelAction.class); /** * @serial Filtro sulle categorie. */ private String myCatgFilter; private transient List<Activity> myActivities; private transient List<String> myCategories; @javax.annotation.Resource(name = WebApplicationManager.BEAN_NAME) private transient WebApplicationManager myApplMgr; /** * Costruttore. */ public ActivityPanelAction() { } /** * Restituisce la categoria selezionata. * * @return Valore. */ public String getCategoryFilter() { return myCatgFilter; } /** * Imposta la categoria selezionata. * * @param value Valore. */ public void setCategoryFilter(String value) { myCatgFilter = value; } /** * Restituisce la collezione delle attività. * * @return Collezione. */ public List<Activity> getActivities() { return myActivities; } /** * Restituisce la collezione delle categorie. * * @return Collezione. */ public List<String> getCategories() { return myCategories; } @Override protected String initViewId() { return ActivityPanelAction.VIEWID; } @Override public String execute() { List<String> applList; applList = myApplMgr.listRunningApplications(); myActivities = listActivities(applList); if (Strings.isNullOrEmpty(myCatgFilter)) { // Ho selezionato tutte le attivita': // Estraggo l'elenco delle categorie dall'elenco delle attivita' // risparmiandomi le chiamate REST. myCategories = listCategories(); } else { myCategories = listCategories(applList); } return Action.SUCCESS; } /** * Elenco delle attività. * * @param applList Applicazioni. * @return Collezione. */ private List<Activity> listActivities(List<String> applList) { String uri; UriComponentsBuilder uriBuilder; ActivityListResource activityRes; List<Activity> activityList, list; activityList = new ArrayList<Activity>(); for (String ctxPath : applList) { uriBuilder = UriComponentsBuilder.fromHttpUrl(myApplMgr.getBaseUrl()); uriBuilder.path(ctxPath).path(ActivityListResource.PATH); uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, getLocale().toString()); if (!Strings.isNullOrEmpty(myCatgFilter)) { uriBuilder.queryParam(ActivityListResource.QUERY_CATEGORY, myCatgFilter); } uri = uriBuilder.build().toUriString(); activityRes = ClientResource.create(uri, ActivityListResource.class); try { list = activityRes.listActivities(); } catch (Exception ex) { myLogger.error(String.format("Failed to get %1$s.", uri), ex); continue; } if (list == null) { myLogger.error("Failed to get {}.", uri); continue; } activityList.addAll(list); } Collections.sort(activityList, new ActivityComparator()); return activityList; } /** * Elenco delle categorie. * * @return Collezione. */ private List<String> listCategories() { List<String> categoryList; Map<String, String> map; map = new HashMap<String, String>(); for (Activity activity : myActivities) { for (String catg : activity.getCategories()) { addCategory(map, Strings.trim(catg)); } } categoryList = new ArrayList<String>(map.values()); Collections.sort(categoryList); return categoryList; } /** * Elenco delle categorie. * * @param applList Applicazioni. * @return Collezione. */ private List<String> listCategories(List<String> applList) { String uri; UriComponentsBuilder uriBuilder; ActivityCategoryListResource catgRes; List<String> categoryList, list; Map<String, String> map; map = new HashMap<String, String>(); for (String ctxPath : applList) { uriBuilder = UriComponentsBuilder.fromHttpUrl(myApplMgr.getBaseUrl()); uriBuilder.path(ctxPath).path(ActivityCategoryListResource.PATH); uriBuilder.queryParam(AbstractServerResource.QUERY_LOCALE, getLocale().toString()); uri = uriBuilder.build().toUriString(); catgRes = ClientResource.create(uri, ActivityCategoryListResource.class); try { list = catgRes.listCategories(); } catch (Exception ex) { myLogger.error(String.format("Failed to get %1$s.", uri), ex); continue; } if (list == null) { myLogger.error("Failed to get {}.", uri); continue; } for (String catg : list) { addCategory(map, Strings.trim(catg)); } } categoryList = new ArrayList<String>(map.values()); Collections.sort(categoryList); return categoryList; } /** * Colleziona una categoria. * * @param map Collezione. * @param catg Categoria. */ private void addCategory(Map<String, String> map, String catg) { String key; if (Strings.isNullOrEmpty(catg)) { return; } key = catg.toLowerCase(); if (map.containsKey(key)) { return; } map.put(key, catg); } }