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.event.dom.client.ClickEvent;
5
import com.google.gwt.event.dom.client.ClickHandler;
6
import com.google.gwt.user.client.History;
7
import com.google.gwt.user.client.rpc.AsyncCallback;
8
import com.google.gwt.user.client.ui.FlowPanel;
9
import com.google.gwt.user.client.ui.HTML;
10
import com.google.gwt.user.client.ui.IsWidget;
11
import com.google.gwt.user.client.ui.Widget;
12
import eu.dnetlib.domain.data.Repository;
13
import eu.dnetlib.repo.manager.client.RepositoryManager;
14
import eu.dnetlib.repo.manager.client.services.RepositoryService;
15
import eu.dnetlib.repo.manager.client.services.RepositoryServiceAsync;
16
import org.gwtbootstrap3.client.ui.*;
17
import org.gwtbootstrap3.client.ui.constants.AlertType;
18
import org.gwtbootstrap3.client.ui.constants.ButtonType;
19
import org.gwtbootstrap3.client.ui.constants.ModalBackdrop;
20
import org.gwtbootstrap3.client.ui.constants.Toggle;
21

    
22
/**
23
 * Created by stefania on 12/19/16.
24
 */
25
public class EnableMetricsWidget implements IsWidget {
26

    
27
    private FlowPanel enableMetricsPanel = new FlowPanel();
28
    private Alert errorAlert = new Alert();
29

    
30
    private FlowPanel modal = new FlowPanel();
31

    
32
    private RepositoryServiceAsync repositoryService = GWT.create(RepositoryService.class);
33

    
34
    public EnableMetricsWidget(final Repository repository) {
35

    
36
        errorAlert.setDismissable(false);
37
        errorAlert.setType(AlertType.DANGER);
38
        errorAlert.setVisible(false);
39
        enableMetricsPanel.add(errorAlert);
40

    
41
        HTML enableMetricsText = new HTML("<h4>You don't have metrics enabled for this repository yet. Would you like to " +
42
                "enable them?</h4>" +
43
                "<p><img src=\"./img/metricsWorkflow-new.svg\" style=\"display:block; margin: 0 auto;\" width=\"50%\"></p>" +
44

    
45
                "<p>Once you select to enable metrics for your repository, the following steps need to be performed:</p>" +
46
                "<p><i>On your side</i> <br>" +
47
                "1. Download the tracking code for your repository platform <br>" +
48
                "2. Configure the tracking code according to the instructions <br>" +
49
                "3. Deploy the tracking code in your repository platform</p>" +
50

    
51
                "<p><i>On the OpenAIRE's side</i> <br>" +
52
                "4. Validate the installation of the tracking code and inform the repository manager accordingly</p>" +
53

    
54
//                "<p>Usage Statistics about items and content providers support the assessment of their popularity and use. " +
55
//                "They are an important complement to other kinds of metrics.</p>" +
56
//                "<p>OpenAIRE Usage Statistics aims to gather usage events (metadata views and downloads) of items from " +
57
//                "participating content providers. By utilizing open standards and protocols reliable, consolidated and " +
58
//                "comparable usage reports are generated that are conformant to the COUNTER Code of Practice.</p>" +
59
//                "<p>To participate in this service Content Provider Managers need to install platform specific patches or " +
60
//                "plugins that enable the tracking of usage events to the OpenAIRE Usage Statistics Service.</p>" +
61
//                "<p>Usage statistics are presented in the Content Provider Dashboard, in the OpenAIRE portal and can be " +
62
//                "retrieved by the OpenAIRE API.</p>" +
63

    
64
                "<div class=\"uk-alert uk-alert-info\">For more details about the workflows and tools please consult the <a target=\"_blank\" " +
65
                "href=\"./pdf/OpenAIRE_usage_stats_Guidelinesv1.pdf\">“Guidelines for Collecting Usage Events and Provision " +
66
                "of Usage Statistics”</a>.</div>");
67

    
68
        enableMetricsPanel.add(enableMetricsText);
69

    
70
        final Button enableMetricsButton = new Button();
71
        enableMetricsButton.setType(ButtonType.PRIMARY);
72
        enableMetricsButton.setText("Enable Metrics");
73
        enableMetricsButton.addClickHandler(new ClickHandler() {
74

    
75
            @Override
76
            public void onClick(ClickEvent event) {
77

    
78
                errorAlert.setVisible(false);
79
                enableMetrics(repository);
80
            }
81
        });
82
        enableMetricsPanel.add(enableMetricsButton);
83

    
84
//        UsageStatisticsGuidelinesWidget usageStatisticsGuidelinesWidget = new UsageStatisticsGuidelinesWidget();
85
//        enableMetricsPanel.add(usageStatisticsGuidelinesWidget.asWidget());
86
    }
87

    
88
    @Override
89
    public Widget asWidget() {
90
        return enableMetricsPanel;
91
    }
92

    
93
    private void enableMetrics(final Repository repository) {
94

    
95
        final Modal enableMetricsConfirmationPopup = new Modal();
96
        enableMetricsConfirmationPopup.setDataBackdrop(ModalBackdrop.STATIC);
97
//        enableMetricsConfirmationPopup.removeStyleName("modal");
98

    
99
        FlowPanel modalContent = new FlowPanel();
100

    
101
        HTML description = new HTML("<h2 class=\"uk-modal-title\">Confirmation</h2>\n" +
102
                "<p>Are you sure you want to enable metrics for this repository?</p>");
103
        modalContent.add(description);
104

    
105
        FlowPanel actionButtons = new FlowPanel();
106
        actionButtons.addStyleName("uk-text-right");
107
        modalContent.add(actionButtons);
108

    
109
        Button no = new Button("Cancel");
110
        no.setType(ButtonType.DEFAULT);
111
        no.addClickHandler(new ClickHandler() {
112
            @Override
113
            public void onClick(ClickEvent event) {
114
                enableMetricsConfirmationPopup.hide();
115
                enableMetricsConfirmationPopup.removeFromParent();
116
            }
117
        });
118
        actionButtons.add(no);
119

    
120
        Button yes = new Button("Yes, enable them");
121
        yes.setType(ButtonType.PRIMARY);
122
        yes.addClickHandler(new ClickHandler() {
123
            @Override
124
            public void onClick(ClickEvent event) {
125

    
126
                enableMetricsConfirmationPopup.hide();
127
                enableMetricsConfirmationPopup.removeFromParent();
128

    
129
                final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
130
                enableMetricsPanel.addStyleName("loading-big");
131
                enableMetricsPanel.add(loadingWheel);
132

    
133
                repositoryService.enableMetricsForRepository(repository, RepositoryManager.currentUser, new AsyncCallback<Void>() {
134

    
135
                    @Override
136
                    public void onFailure(Throwable caught) {
137

    
138
                        enableMetricsPanel.removeStyleName("loading-big");
139
                        enableMetricsPanel.remove(loadingWheel);
140

    
141
                        errorAlert.setText("System error enabling metrics for this repository.");
142
                        errorAlert.setVisible(true);
143
                    }
144

    
145
                    @Override
146
                    public void onSuccess(Void result) {
147

    
148
                        enableMetricsPanel.removeStyleName("loading-big");
149
                        enableMetricsPanel.remove(loadingWheel);
150

    
151
                        History.newItem("getImpact/instructions/" + repository.getId());
152
                    }
153
                });
154
            }
155
        });
156
        actionButtons.add(yes);
157

    
158
        ModalBody modalBody = new ModalBody();
159
        modalBody.add(modalContent);
160
        enableMetricsConfirmationPopup.add(modalBody);
161
        enableMetricsConfirmationPopup.getElement().getChild(0).getChild(0).getChild(0).removeFromParent();
162

    
163
        enableMetricsConfirmationPopup.show();
164
    }
165
}
(1-1/6)