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.Date;
21
import java.util.logging.Level;
22
import java.util.logging.Logger;
23

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

    
26

    
27
public class RepositoryManager implements EntryPoint {
28

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

    
32
    public static String publicCaptchaKey = "";
33

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

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

    
39
    public void onModuleLoad() {
40

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

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

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

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

    
52
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
53
            @Override
54
            public void onValueChange(ValueChangeEvent<String> event) {
55
                String historyToken = event.getValue();
56
                if(historyToken!=null && !historyToken.equals("")) {
57
                    String finalHistoryToken = getFinalHistoryToken(historyToken);
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
                Cookies.setCookie("currentUser","",new Date(0));
76
                History.newItem("landing");
77
                NavigationManager.getInstance().navigate("landing");
78
            }
79
            @Override
80
            public void onSuccess(Tuple<UserProfile, String> result) {
81
                RepositoryManager.currentUser = result.getFirst();
82
                RepositoryManager.currentUserRole = result.getSecond();
83

    
84
                if (RepositoryManager.currentUser != null)
85
                    Document.get().getElementById("currentUser").setInnerText(RepositoryManager.currentUser.getFirstname()
86
                            + " " + RepositoryManager.currentUser.getLastname());
87

    
88
                //TODO use role instead of email
89
                if(RepositoryManager.currentUserRole.equals("admin")) {
90
                    Document.get().getElementById("admin1").getStyle().setDisplay(Style.Display.BLOCK);
91
                    Document.get().getElementById("admin2").getStyle().setDisplay(Style.Display.BLOCK);
92
                }
93
                History.newItem(nextPage);
94
                NavigationManager.getInstance().navigate(nextPage);
95
            }
96
        });
97
    }
98

    
99
    private void addLogoutHandler() {
100

    
101
        $(".logoutLink").click(new Function() {
102

    
103
            public boolean f(Event e) {
104

    
105
                Cookies.setCookie("currentUser","",new Date(0));
106

    
107
                userService.clearCookie(new AsyncCallback<Void>() {
108
                    @Override
109
                    public void onFailure(Throwable throwable) {
110
                        Window.alert("Fail to clear cookie!");
111
                    }
112

    
113
                    @Override
114
                    public void onSuccess(Void aVoid) {
115
                        RepositoryManager.currentUser = null;
116
                        RepositoryManager.currentUserRole = null;
117
                        Window.Location.replace("https://aai.openminted.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo="+
118
                        GWT.getHostPageBaseURL() + "#landing");
119
                    }
120
                });
121
                return false;
122
            }
123
        });
124
    }
125

    
126
    private String getFinalHistoryToken(String historyToken) {
127

    
128
        if(RepositoryManager.currentUser==null) {
129

    
130
            if(!(historyToken.equals("landing") || historyToken.equals("login") || historyToken.equals("register"))) {
131
                historyToken = "landing";
132
            }
133

    
134
        } else if(!RepositoryManager.currentUserRole.equals("admin")) {
135
            if(historyToken.equals("admin/helpTexts") || historyToken.equals("admin/metrics")) {
136
                historyToken = "dashboard";
137
            }
138
        }
139

    
140
        return historyToken;
141
    }
142
}
(8-8/12)