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

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

Unit test for getting profiles and components from groups. A little bug is fixed

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