source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/frontend/StatisticsPage.java @ 5549

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 5.7 KB
Line 
1package clarin.cmdi.componentregistry.frontend;
2
3import java.io.IOException;
4import java.util.List;
5
6import org.apache.wicket.PageParameters;
7import org.apache.wicket.markup.html.WebMarkupContainer;
8import org.apache.wicket.markup.html.basic.Label;
9import org.apache.wicket.markup.html.link.Link;
10import org.apache.wicket.markup.repeater.RepeatingView;
11import org.apache.wicket.spring.injection.annot.SpringBean;
12
13import clarin.cmdi.componentregistry.ComponentRegistry;
14import clarin.cmdi.componentregistry.ComponentRegistryException;
15import clarin.cmdi.componentregistry.ComponentRegistryFactory;
16import clarin.cmdi.componentregistry.RegistrySpace;
17import clarin.cmdi.componentregistry.UserUnauthorizedException;
18import clarin.cmdi.componentregistry.components.CMDComponentSpec;
19import clarin.cmdi.componentregistry.components.CMDComponentType;
20import clarin.cmdi.componentregistry.components.CMDElementType;
21import clarin.cmdi.componentregistry.impl.database.CMDComponentSpecExpanderDbImpl;
22import clarin.cmdi.componentregistry.impl.database.ComponentRegistryDbImpl;
23import clarin.cmdi.componentregistry.model.ComponentDescription;
24import clarin.cmdi.componentregistry.model.ProfileDescription;
25import java.util.Collection;
26import java.util.Collections;
27
28/**
29 *
30 * @author paucas
31 */
32public class StatisticsPage extends SecureAdminWebPage {
33
34    private static final long serialVersionUID = 1L;
35    @SpringBean(name = "componentRegistryFactory")
36    private ComponentRegistryFactory componentRegistryFactory;
37
38    private static class Statistics {
39        private int componentnumber = 0;
40        private int elementcounter = 0;
41        private int conceptlinkcounter = 0;
42    }
43
44    public StatisticsPage(final PageParameters pageParameters) throws IOException, ComponentRegistryException, UserUnauthorizedException {
45        super(pageParameters);
46        ComponentRegistry registry = componentRegistryFactory.getComponentRegistry(RegistrySpace.PUBLISHED, null, null, null);
47        addLinks();
48        displayStatistics(registry);
49    }
50
51    private void displayStatistics(ComponentRegistry registry) throws ComponentRegistryException, UserUnauthorizedException {
52        List<ProfileDescription> profileList = registry.getProfileDescriptions();
53        RepeatingView repeating = new RepeatingView("repeating");
54        add(repeating);
55        add(new Label("profilenumbermessage", "Current number of profiles in the component registry: " + profileList.size()));
56        for (ProfileDescription pd : profileList) {
57            displayProfileStatistics(pd, repeating, registry);
58        }
59        List<ComponentDescription> componentList = registry.getComponentDescriptions();
60        RepeatingView repeatingcomp = new RepeatingView("repeatingcomp");
61        add(repeatingcomp);
62        add(new Label("componentnumbermessage", "Current number of components in the component registry: " + componentList.size()));
63        for (ComponentDescription cd : componentList) {
64            displayComponentStatistics(cd, repeatingcomp, registry);
65        }
66    }
67
68    private void displayProfileStatistics(ProfileDescription pd, RepeatingView repeatingview, ComponentRegistry registry) throws ComponentRegistryException {
69        WebMarkupContainer item = new WebMarkupContainer(repeatingview.newChildId());
70        repeatingview.add(item);
71        item.add(new Label("ID", pd.getId()));
72        item.add(new Label("profname", pd.getName()));
73        CMDComponentSpec profile = CMDComponentSpecExpanderDbImpl.expandProfile(pd.getId(), (ComponentRegistryDbImpl) registry);
74        Statistics stats = new Statistics();
75        componentCounter(profile, stats);
76        item.add(new Label("nrcomp", "" + stats.componentnumber));
77        item.add(new Label("nrprofelem", "" + stats.elementcounter));
78        item.add(new Label("nrproflinks", "" + stats.conceptlinkcounter));
79    }
80
81    private void displayComponentStatistics(ComponentDescription cd, RepeatingView repeatingview,  ComponentRegistry registry) throws ComponentRegistryException {
82        WebMarkupContainer item = new WebMarkupContainer(repeatingview.newChildId());
83        repeatingview.add(item);
84        item.add(new Label("ID", cd.getId()));
85        item.add(new Label("compname", cd.getName()));
86        CMDComponentSpec compspec = CMDComponentSpecExpanderDbImpl.expandComponent(cd.getId(), (ComponentRegistryDbImpl) registry);
87        Statistics stats = new Statistics();
88        componentCounter(compspec, stats);
89        item.add(new Label("nrcomp", "" + stats.componentnumber));
90        item.add(new Label("nrelem", "" + stats.elementcounter));
91        item.add(new Label("nrcomplinks", "" + stats.conceptlinkcounter));
92    }
93
94    private void componentCounter(CMDComponentSpec components, Statistics stats) {
95        componentCounter(Collections.singleton(components.getCMDComponent()), stats);
96    }
97   
98    private void componentCounter(Collection<CMDComponentType> components, Statistics stats) {
99        if (components != null) {
100            for (CMDComponentType component : components) {
101                stats.componentnumber++;
102                List<CMDElementType> elementlist = component.getCMDElement();
103                if (elementlist != null) {
104                    stats.elementcounter += elementlist.size();
105                    for (CMDElementType elem : elementlist) {
106                        if (elem.getConceptLink() != null) {
107                            stats.conceptlinkcounter++;
108                        }
109                    }
110                }
111                componentCounter(component.getCMDComponent(), stats);
112            }
113        }
114    }
115
116    @SuppressWarnings("serial")
117    private void addLinks() {
118        add(new Link("home") {
119
120            @Override
121            public void onClick() {
122                setResponsePage(AdminHomePage.class);
123            }
124        });
125    }
126}
Note: See TracBrowser for help on using the repository browser.