Project

General

Profile

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

    
3
import com.google.gwt.core.client.EntryPoint;
4
import com.google.gwt.core.client.GWT;
5
import com.google.gwt.dom.client.Document;
6
import com.google.gwt.dom.client.Style;
7
import com.google.gwt.event.logical.shared.ValueChangeEvent;
8
import com.google.gwt.event.logical.shared.ValueChangeHandler;
9
import com.google.gwt.query.client.Function;
10
import com.google.gwt.user.client.Cookies;
11
import com.google.gwt.user.client.Event;
12
import com.google.gwt.user.client.History;
13
import com.google.gwt.user.client.Window;
14
import com.google.gwt.user.client.rpc.AsyncCallback;
15
import com.google.gwt.user.client.ui.FlowPanel;
16
import com.google.gwt.user.client.ui.RootPanel;
17
import eu.dnetlib.domain.functionality.UserProfile;
18
import eu.dnetlib.repo.manager.client.services.UserService;
19
import eu.dnetlib.repo.manager.client.services.UserServiceAsync;
20
import eu.dnetlib.repo.manager.shared.Tuple;
21
import eu.dnetlib.repo.manager.shared.UserAccessException;
22

    
23
import java.util.logging.Level;
24
import java.util.logging.Logger;
25

    
26
import static com.google.gwt.query.client.GQuery.$;
27

    
28

    
29
public class RepositoryManager implements EntryPoint {
30

    
31
    public static UserProfile currentUser = null;
32
    public static String currentUserRole = null;
33

    
34
    public static String publicCaptchaKey = "";
35

    
36
    public static String currentToken = "landing";
37
    public static String previousToken = "landing";
38

    
39
    private UserServiceAsync userService = GWT.create(UserService.class);
40

    
41
    public void onModuleLoad() {
42

    
43
        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
44

    
45
            Logger logger = Logger.getLogger("UncaughtException");
46

    
47
            @Override
48
            public void onUncaughtException(Throwable arg0) {
49

    
50
                logger.log(Level.SEVERE, "Exception: ", arg0);
51
            }
52
        });
53

    
54
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
55
            @Override
56
            public void onValueChange(ValueChangeEvent<String> event) {
57

    
58
                String historyToken = event.getValue();
59

    
60
                if(historyToken!=null && !historyToken.equals("")) {
61

    
62
                    String finalHistoryToken = getFinalHistoryToken(historyToken);
63

    
64
                    History.newItem(finalHistoryToken);
65
                    NavigationManager.getInstance().navigate(finalHistoryToken);
66

    
67
                } else {
68

    
69
                    //TODO kapou peftei se loop
70
                    History.newItem("landing");
71
                    NavigationManager.getInstance().navigate("landing");
72
                }
73
            }
74
        });
75

    
76
        String hash = Window.Location.getHash();
77
        String activationId = Window.Location.getParameter("activationId");
78
        final String hashValue = hash.substring(1);
79

    
80
        if(hashValue.startsWith("activateAccount")) {
81

    
82
            Cookies.removeCookie("currentUser");
83

    
84
            if(activationId!=null) {
85

    
86
                userService.activateUser(activationId, new AsyncCallback<Void>() {
87

    
88
                    @Override
89
                    public void onFailure(Throwable throwable) {
90

    
91
                        if(throwable instanceof UserAccessException) {
92
                            UserAccessException uae = (UserAccessException) throwable;
93
                            LoginPage.getInstance().showSuccessfulMessage(uae.getMessage());
94
                            Window.Location.replace(GWT.getHostPageBaseURL() + "#login");
95
                        } else {
96
                            //TODO
97
                        }
98
                    }
99

    
100
                    @Override
101
                    public void onSuccess(Void aVoid) {
102

    
103
                        //TODO it does not show the message because it refreshes the page
104
                        LoginPage.getInstance().showSuccessfulMessage("Account activation was successful! You can now log-in " +
105
                                "using your account details.");
106
                        Window.Location.replace(GWT.getHostPageBaseURL() + "#login");
107
//                        NavigationManager.getInstance().navigate("login");
108
//                        Window.Location.replace(GWT.getHostPageBaseURL() + "#login");
109
                    }
110
                });
111

    
112
            } else {
113

    
114
                //TODO invalid url
115
            }
116

    
117
        } else {
118

    
119
            String encryptedEmail = Cookies.getCookie("currentUser");
120
            if (encryptedEmail == null) {
121

    
122
                String finalHistoryToken = getFinalHistoryToken(hashValue);
123
                History.newItem(finalHistoryToken);
124
                NavigationManager.getInstance().navigate(finalHistoryToken);
125

    
126
            } else {
127

    
128
                userService.getUserByEmail(Crypto.decrypt(encryptedEmail), new AsyncCallback<Tuple<UserProfile, String>>() {
129

    
130
                    @Override
131
                    public void onFailure(Throwable caught) {
132

    
133
                        Cookies.removeCookie("currentUser");
134

    
135
                        String finalHistoryToken = getFinalHistoryToken(hashValue);
136
                        History.newItem(finalHistoryToken);
137
                        NavigationManager.getInstance().navigate(finalHistoryToken);
138
                    }
139

    
140
                    @Override
141
                    public void onSuccess(Tuple<UserProfile, String> result) {
142

    
143
                        RepositoryManager.currentUser = result.getFirst();
144
                        RepositoryManager.currentUserRole = result.getSecond();
145

    
146
                        if (RepositoryManager.currentUser != null)
147
                            Document.get().getElementById("currentUser").setInnerText(RepositoryManager.currentUser.getFirstname()
148
                                    + " " + RepositoryManager.currentUser.getLastname());
149

    
150
                        //TODO use role instead of email
151
                        if(RepositoryManager.currentUserRole.equals("admin")) {
152
                            Document.get().getElementById("admin1").getStyle().setDisplay(Style.Display.BLOCK);
153
                            Document.get().getElementById("admin2").getStyle().setDisplay(Style.Display.BLOCK);
154
                        }
155

    
156
                        String finalHistoryToken = getFinalHistoryToken(hashValue);
157
                        History.newItem(finalHistoryToken);
158
                        NavigationManager.getInstance().navigate(finalHistoryToken);
159
                    }
160
                });
161
            }
162
        }
163

    
164
        addLogoutHandler();
165
    }
166

    
167
    private void addLogoutHandler() {
168

    
169
        $(".logoutLink").click(new Function() {
170

    
171
            public boolean f(Event e) {
172

    
173
                Cookies.removeCookie("currentUser");
174
                RepositoryManager.currentUser = null;
175
                RepositoryManager.currentUserRole = null;
176

    
177
                if(RepositoryManager.currentToken.equals("landing"))
178
                    NavigationManager.getInstance().navigate("landing");
179
                else
180
                    History.newItem("landing");
181

    
182
                return false;
183
            }
184
        });
185
    }
186

    
187
    private String getFinalHistoryToken(String historyToken) {
188

    
189
        if(RepositoryManager.currentUser==null) {
190

    
191
            if(!(historyToken.equals("landing") || historyToken.equals("login") || historyToken.equals("register"))) {
192
                historyToken = "landing";
193
            }
194

    
195
        } else if(!RepositoryManager.currentUserRole.equals("admin")) {
196
            if(historyToken.equals("admin/helpTexts") || historyToken.equals("admin/metrics")) {
197
                historyToken = "dashboard";
198
            }
199
        }
200

    
201
        return historyToken;
202
    }
203
}
(8-8/12)