Project

General

Profile

1
package eu.dnetlib.repo.manager.client.getimpact;
2

    
3
import com.google.gwt.core.client.GWT;
4
import com.google.gwt.dom.client.Document;
5
import com.google.gwt.user.client.Window;
6
import com.google.gwt.user.client.rpc.AsyncCallback;
7
import com.google.gwt.user.client.ui.FlowPanel;
8
import com.google.gwt.user.client.ui.HTML;
9
import eu.dnetlib.domain.data.PiwikInfo;
10
import eu.dnetlib.domain.data.Repository;
11
import eu.dnetlib.gwt.client.help.HelpService;
12
import eu.dnetlib.gwt.client.help.HelpServiceAsync;
13
import eu.dnetlib.repo.manager.client.AbstractRepositoryManagerWidget;
14
import eu.dnetlib.repo.manager.client.HelpCallback;
15
import eu.dnetlib.repo.manager.client.RepositoryManagerWidget;
16
import eu.dnetlib.repo.manager.client.services.RepositoryService;
17
import eu.dnetlib.repo.manager.client.services.RepositoryServiceAsync;
18
import org.gwtbootstrap3.client.ui.Alert;
19
import org.gwtbootstrap3.client.ui.constants.AlertType;
20

    
21
/**
22
 * Created by stefania on 12/19/16.
23
 */
24
public class InstructionsForMetricsWidget extends AbstractRepositoryManagerWidget implements RepositoryManagerWidget {
25

    
26
    private String parentToken = "";
27
    private String restToken = "";
28

    
29
    private String repositoryId = "";
30

    
31
    private FlowPanel instructionsForMetricsBoxContent = new FlowPanel();
32

    
33
    private Alert errorAlert = new Alert();
34

    
35
    private RepositoryServiceAsync repositoryService = GWT.create(RepositoryService.class);
36
    private static HelpServiceAsync helpService = GWT.create(HelpService.class);
37

    
38
    public InstructionsForMetricsWidget() {
39

    
40
        super();
41

    
42
        HTML title = new HTML("<h1 class=\"uk-article-title\">Metrics Configuration & Software Details</h1>");
43
        title.addStyleName("uk-margin-medium-bottom");
44
        contentPanel.add(title);
45

    
46
        errorAlert.setType(AlertType.DANGER);
47
        errorAlert.setDismissable(false);
48
        errorAlert.setVisible(false);
49
        instructionsForMetricsBoxContent.add(errorAlert);
50

    
51
        contentPanel.add(instructionsForMetricsBoxContent);
52
    }
53

    
54
    @Override
55
    public void clear() {
56

    
57
        instructionsForMetricsBoxContent.clear();
58
        errorAlert.setVisible(false);
59
        gridPanel.remove(helpPanel);
60
    }
61

    
62
    @Override
63
    public void reload() {
64

    
65
        Document.get().getElementById("headerNoTransparent").removeClassName("tm-header-transparent");
66
        instructionsForMetricsBoxContent.add(errorAlert);
67

    
68
        helpService.getHelpById(parentToken + "_forDatasource_instructions", new HelpCallback(helpPanel, gridPanel));
69

    
70
        final HTML loadingWheel = new HTML("<div class=\"loader-big\" style=\"text-align: center; padding-top: 170px; " +
71
                "color: rgb(47, 64, 80); font-weight: bold;\">Retrieving repository information...</div>" +
72
                "<div class=\"whiteFilm\"></div>");
73
        instructionsForMetricsBoxContent.addStyleName("loading-big");
74
        instructionsForMetricsBoxContent.add(loadingWheel);
75

    
76
        repositoryService.getRepository(repositoryId, new AsyncCallback<Repository>() {
77

    
78
            @Override
79
            public void onFailure(Throwable caught) {
80

    
81
                instructionsForMetricsBoxContent.removeStyleName("loading-big");
82
                instructionsForMetricsBoxContent.remove(loadingWheel);
83

    
84
                errorAlert.setText("System error retrieving repository information");
85
                errorAlert.setVisible(true);
86
            }
87

    
88
            @Override
89
            public void onSuccess(final Repository repository) {
90

    
91

    
92
                repositoryService.getPiwikSiteForRepository(repository.getId(), new AsyncCallback<PiwikInfo>() {
93

    
94
                    @Override
95
                    public void onFailure(Throwable throwable) {
96

    
97
                        instructionsForMetricsBoxContent.removeStyleName("loading-big");
98
                        instructionsForMetricsBoxContent.remove(loadingWheel);
99

    
100
                        errorAlert.setText("System error retrieving the piwik info for this repository");
101
                        errorAlert.setVisible(true);
102
                    }
103

    
104
                    @Override
105
                    public void onSuccess(PiwikInfo piwikInfo) {
106

    
107
                        instructionsForMetricsBoxContent.add(showInstructions(piwikInfo));
108

    
109
                        instructionsForMetricsBoxContent.removeStyleName("loading-big");
110
                        instructionsForMetricsBoxContent.remove(loadingWheel);
111

    
112
                        Window.moveTo(0,0);
113
                    }
114
                });
115

    
116
//                repositoryService.getPiwikScriptForRepository(repositoryId, new AsyncCallback<String>() {
117
//
118
//                    @Override
119
//                    public void onFailure(Throwable caught) {
120
//
121
//                        instructionsForMetricsBoxContent.removeStyleName("loading-big");
122
//                        instructionsForMetricsBoxContent.remove(loadingWheel);
123
//
124
//                        errorAlert.setText("System error retrieving piwik script for this repository");
125
//                        errorAlert.setVisible(true);
126
//                    }
127
//
128
//                    @Override
129
//                    public void onSuccess(String piwikScript) {
130
//
131
////                        RepositoryManager.pageHeader.clear();
132
////                        String logoURLSource = "";
133
////                        if(repository.getLogoUrl()==null || repository.getLogoUrl().isEmpty())
134
////                            logoURLSource = "img/yourLogoHere.jpg";
135
////                        else
136
////                            logoURLSource = repository.getLogoUrl();
137
////
138
////                        RepositoryManager.pageHeader.add(new HTML("<div class=\"col-sm-9\"><h2><img src=\"" + logoURLSource + "\" alt=[Repo Logo]" +
139
////                                "width=\"35px\" height=\"35px\" style=\"margin-right: 15px; margin-top:-2px\">Enable metrics for " + repository.getOfficialName() + "</h2>" +
140
////                                "<ol class=\"breadcrumb\"><li><a href=\"#dashboard\">Home</a></li><li><a href=\"#" + parentToken +
141
////                                "\">Metrics</a></li><li><a href=\"#" + parentToken + "/" + repositoryId + "\">" + repository.getOfficialName() + "</a>" +
142
////                                "</li><li class=\"active\"><strong>Instructions</strong></li></ol></div><div class=\"col-sm-3\"></div>"));
143
//
144
//                        instructionsForMetricsBoxContent.removeStyleName("loading-big");
145
//                        instructionsForMetricsBoxContent.remove(loadingWheel);
146
//
147
//                        if(piwikScript!=null)
148
//                            instructionsForMetricsBoxContent.add(showInstructions(piwikScript));
149
//                        else {
150
//
151
//                            Alert infoAlert = new Alert();
152
//                            infoAlert.setType(AlertType.WARNING);
153
//                            infoAlert.setDismissable(false);
154
//                            infoAlert.setText("Metrics have not yet been enabled for this repository.");
155
//                            instructionsForMetricsBoxContent.add(infoAlert);
156
//
157
//                            Button goToEnableMetrics = new Button();
158
//                            goToEnableMetrics.setText("I would like to enable metrics");
159
//                            goToEnableMetrics.setType(ButtonType.PRIMARY);
160
//                            goToEnableMetrics.addClickHandler(new ClickHandler() {
161
//
162
//                                @Override
163
//                                public void onClick(ClickEvent event) {
164
//                                    History.newItem("getImpact/" + repository.getId());
165
//                                }
166
//                            });
167
//                            instructionsForMetricsBoxContent.add(goToEnableMetrics);
168
//                        }
169
//                    }
170
//                });
171
            }
172
        });
173
    }
174

    
175
    @Override
176
    public void setToken(String parentToken, String rest) {
177
        this.parentToken = parentToken;
178
        this.restToken = rest;
179

    
180
        String[] tokenSplit = restToken.split("/", 2);
181
        repositoryId = tokenSplit[1];
182
    }
183

    
184
    @Override
185
    public void afterAdditionToRootPanel() {
186

    
187
    }
188

    
189
    private FlowPanel showInstructions(PiwikInfo piwikInfo) {
190

    
191
        FlowPanel instructionsPanel = new FlowPanel();
192

    
193
        String instructions = "<p>OpenAIRE’s usage statistic service uses the <i>Piwik Open Source Analytics platform</i> (piwik.org)  " +
194
                "to track usage activity. When <i>metrics</i> are enabled for a repository, two unique identifiers are generated - " +
195
                "a piwik-ID that associates the repository with its usage events in Piwik and an authentication-ID that allows " +
196
                "to track usage activity on the Piwik platform. Metadata views and item downloads are tracked and automatically " +
197
                "sent to Piwik. Statistics are generated using the COUNTER Code of practice directives.</p>";
198

    
199
        instructions += "OpenAIRE's usage statistics service tracking code exploits Piwik’s API. In order to make the " +
200
                "tracking of usage events from repositories more robust, it was necessary to implement repository platform " +
201
                "specific patches and plugins starting with DSpace and EPrints. The code is maintained on Github:";
202

    
203
        instructions += "<ul>" +
204
                "<li>as a patch for various versions of DSpace (<a target=\"_blank\" " +
205
                "href=\"https://github.com/openaire/OpenAIRE-Piwik-DSpace\">https://github.com/openaire/OpenAIRE-Piwik-DSpace</a>)</li>" +
206
                "<li>as an EPrints plugin for version 3 (<a target=\"_blank\" " +
207
                "href=\"https://github.com/openaire/EPrints-OAPiwik\">https://github.com/openaire/EPrints-OAPiwik</a>)</li>" +
208
                "</ul>";
209

    
210
        instructions += "<p>To configure your repository to allow tracking in Piwik platform, please change the configuration " +
211
                "files with the following parameters and values, generated for your site:</p>" +
212
                "<dl class=\"uk-description-list\">" +
213
                "    <dt>PiwikID</dt>\n" +
214
                "    <dd>" + piwikInfo.getSiteId() + "</dd>\n" +
215
                "    <dt>AuthenticationToken</dt>\n" +
216
                "    <dd>" + piwikInfo.getAuthenticationToken() + "</dd>\n" +
217
                "</dl>";
218

    
219
        instructions += "<p>Details for the configuration files are given in the README of the tracking code.</p>";
220

    
221
        instructions += "<div class=\"uk-alert uk-alert-info\"><strong>NOTE</strong> - You will be informed by e-mail that the installation of the tracking code has been validated and when the usage statistics will be available.</div>";
222

    
223
        HTML instructionsHTML = new HTML();
224
        instructionsHTML.setHTML(instructions);
225
        instructionsPanel.add(instructionsHTML);
226

    
227
        return  instructionsPanel;
228
    }
229
}
(4-4/6)