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 eu.dnetlib.domain.functionality.UserProfile;
16
import eu.dnetlib.repo.manager.client.services.UserService;
17
import eu.dnetlib.repo.manager.client.services.UserServiceAsync;
18
import eu.dnetlib.repo.manager.shared.Tuple;
19

    
20
import java.util.logging.Level;
21
import java.util.logging.Logger;
22

    
23
import static com.google.gwt.query.client.GQuery.$;
24

    
25

    
26
public class RepositoryManager implements EntryPoint {
27

    
28
    public static UserProfile currentUser = null;
29
    public static String currentUserRole = null;
30

    
31
    public static String publicCaptchaKey = "";
32

    
33
    public static String currentToken = "landing";
34
    public static String previousToken = "landing";
35

    
36
    private UserServiceAsync userService = GWT.create(UserService.class);
37

    
38
    public void onModuleLoad() {
39

    
40
        GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
41

    
42
            Logger logger = Logger.getLogger("UncaughtException");
43

    
44
            @Override
45
            public void onUncaughtException(Throwable arg0) {
46

    
47
                logger.log(Level.SEVERE, "Exception: ", arg0);
48
            }
49
        });
50

    
51
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
52
            @Override
53
            public void onValueChange(ValueChangeEvent<String> event) {
54
                String historyToken = event.getValue();
55
                if(historyToken!=null && !historyToken.equals("")) {
56
                    String finalHistoryToken = getFinalHistoryToken(historyToken);
57
                    Window.alert(finalHistoryToken);
58
                    History.newItem(finalHistoryToken);
59
                    NavigationManager.getInstance().navigate(finalHistoryToken);
60
                }else {
61
                    //TODO kapou peftei se loop
62
                    History.newItem("landing");
63
                    NavigationManager.getInstance().navigate("landing");
64
                }
65
            }
66
        });
67
        checkCookie("dashboard");
68
        addLogoutHandler();
69
    }
70

    
71
    private void checkCookie(final String nextPage) {
72
        userService.checkCookie(new AsyncCallback<Tuple<UserProfile, String>>() {
73
            @Override
74
            public void onFailure(Throwable throwable) {
75
                if(throwable instanceof ClassCastException)
76
                    Window.alert("Authentication class exception");
77
                else
78
                    Window.alert("User not found exception");
79

    
80
                Cookies.removeCookie("currentUser");
81

    
82
                //String finalHistoryToken = getFinalHistoryToken(hashValue);
83
                History.newItem("landing");
84
                NavigationManager.getInstance().navigate("landing");
85
            }
86
            @Override
87
            public void onSuccess(Tuple<UserProfile, String> result) {
88
                RepositoryManager.currentUser = result.getFirst();
89
                RepositoryManager.currentUserRole = result.getSecond();
90

    
91
                if (RepositoryManager.currentUser != null)
92
                    Document.get().getElementById("currentUser").setInnerText(RepositoryManager.currentUser.getFirstname()
93
                            + " " + RepositoryManager.currentUser.getLastname());
94

    
95
                //TODO use role instead of email
96
                if(RepositoryManager.currentUserRole.equals("admin")) {
97
                    Document.get().getElementById("admin1").getStyle().setDisplay(Style.Display.BLOCK);
98
                    Document.get().getElementById("admin2").getStyle().setDisplay(Style.Display.BLOCK);
99
                }
100

    
101
                //String finalHistoryToken = getFinalHistoryToken(page);
102
                History.newItem(nextPage);
103
                NavigationManager.getInstance().navigate(nextPage);
104
            }
105
        });
106
    }
107

    
108
    private void addLogoutHandler() {
109

    
110
        $(".logoutLink").click(new Function() {
111

    
112
            public boolean f(Event e) {
113

    
114
                Cookies.removeCookie("currentUser");
115
                userService.clearCookie(new AsyncCallback<Void>() {
116
                    @Override
117
                    public void onFailure(Throwable throwable) {
118

    
119
                    }
120

    
121
                    @Override
122
                    public void onSuccess(Void aVoid) {
123
                        RepositoryManager.currentUser = null;
124
                        RepositoryManager.currentUserRole = null;
125

    
126
                        if(RepositoryManager.currentToken.equals("landing"))
127
                            NavigationManager.getInstance().navigate("landing");
128
                        else
129
                            History.newItem("landing");
130
                    }
131
                });
132
                return false;
133
            }
134
        });
135
    }
136

    
137
    private String getFinalHistoryToken(String historyToken) {
138

    
139
        if(RepositoryManager.currentUser==null) {
140

    
141
            if(!(historyToken.equals("landing") || historyToken.equals("login") || historyToken.equals("register"))) {
142
                historyToken = "landing";
143
            }
144

    
145
        } else if(!RepositoryManager.currentUserRole.equals("admin")) {
146
            if(historyToken.equals("admin/helpTexts") || historyToken.equals("admin/metrics")) {
147
                historyToken = "dashboard";
148
            }
149
        }
150

    
151
        return historyToken;
152
    }
153
}
(8-8/12)