Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.google.gwt.core.client.EntryPoint;
4
import com.google.gwt.core.client.GWT;
5
import com.google.gwt.event.logical.shared.ValueChangeEvent;
6
import com.google.gwt.event.logical.shared.ValueChangeHandler;
7
import com.google.gwt.user.client.Cookies;
8
import com.google.gwt.user.client.History;
9
import com.google.gwt.user.client.Window;
10
import com.google.gwt.user.client.rpc.AsyncCallback;
11
import com.google.gwt.user.client.ui.RootPanel;
12
import eu.dnetlib.goldoa.domain.Person;
13
import eu.dnetlib.goldoa.domain.PersonRole;
14
import eu.dnetlib.goldoa.domain.Role;
15
import eu.dnetlib.shared.InitLoad;
16

    
17
import java.util.ArrayList;
18
import java.util.List;
19

    
20

    
21
public class GoldOAPortal implements EntryPoint {
22

    
23
    public static Person currentUser = null;
24

    
25
    public static String currentToken = "home";
26
    public static String previousToken = "home";
27

    
28
    public static List<Role> userRoles = new ArrayList<>();
29

    
30
    private DataServiceAsync dataService = GWT.create(DataService.class);
31

    
32
    public void onModuleLoad() {
33

    
34
        RootPanel.get("loginRegister").add(RegisterLoginElement.getInstance());
35

    
36
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
37
            @Override
38
            public void onValueChange(ValueChangeEvent<String> event) {
39

    
40
                String historyToken = event.getValue();
41

    
42
                if(historyToken!=null && !historyToken.equals("")) {
43

    
44
                    if(GoldOAPortal.currentUser==null) {
45
                        if(!(historyToken.equals("home") || historyToken.equals("help") || historyToken.equals("login") || historyToken.equals("register"))) {
46
                            historyToken = "home";
47
                        }
48
                    } else {
49

    
50
                        boolean isModeratorOrAdmin = false;
51
                        for(PersonRole personRole : GoldOAPortal.currentUser.getRoles()) {
52
                            if(personRole.getRole().getId().equals("moderator") && personRole.isApproved()) {
53
                                isModeratorOrAdmin = true;
54
                            }
55
                            if(personRole.getRole().getId().equals("administrator") && personRole.isApproved()) {
56
                                isModeratorOrAdmin = true;
57
                            }
58
                        }
59
                        if((!isModeratorOrAdmin && historyToken.equals("monitorFundingRequests"))
60
                                || (!isModeratorOrAdmin && historyToken.equals("monitorBudgets"))) {
61
                            historyToken = "home";
62
                        }
63
                    }
64

    
65
                    History.newItem(historyToken);
66
                    NavigationManager.getInstance().navigate(historyToken);
67

    
68
                } else {
69

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

    
77
        String hash = Window.Location.getHash();
78
        String email = Window.Location.getParameter("email");
79
        String token = Window.Location.getParameter("token");
80
        String encryptedEmail = Cookies.getCookie("currentUser");
81
        String cookieEmail = null;
82
        if(encryptedEmail!=null)
83
            cookieEmail = Crypto.decrypt(encryptedEmail);
84

    
85
        dataService.load(hash, email, token, cookieEmail, new AsyncCallback<InitLoad>() {
86

    
87
                    @Override
88
                    public void onFailure(Throwable throwable) {
89
                        //TODO system error
90
                    }
91

    
92
                    @Override
93
                    public void onSuccess(InitLoad initLoad) {
94

    
95
                        GoldOAPortal.currentUser = null;
96
                        Cookies.removeCookie("currentUser");
97

    
98
                        GoldOAPortal.userRoles = initLoad.getUserRoles();
99

    
100
                        if(initLoad.getPerson()!=null) {
101

    
102
                            //TODO if it was activate, remove parameters from url
103

    
104
                            GoldOAPortal.currentUser = initLoad.getPerson();
105
                            Cookies.setCookie("currentUser", Crypto.encrypt(initLoad.getPerson().getEmail()));
106

    
107
                            UserInfoElement userInfoElement = new UserInfoElement();
108
                            RootPanel.get("loginRegister").clear();
109
                            RootPanel.get("loginRegister").add(userInfoElement.asWidget());
110

    
111
                            NavigationManager.getInstance().update();
112
                            History.newItem(initLoad.getGoTo());
113
                            NavigationManager.getInstance().navigate(initLoad.getGoTo());
114

    
115
                        } else {
116

    
117
                            if(initLoad.getErrorMessage()!=null) {
118

    
119
                                NavigationManager.getInstance().update();
120
                                History.newItem(initLoad.getGoTo());
121
                                NavigationManager.getInstance().navigate(initLoad.getGoTo());
122
                                //TODO display error message
123

    
124
                            } else {
125

    
126
                                NavigationManager.getInstance().update();
127
                                History.newItem(initLoad.getGoTo());
128
                                NavigationManager.getInstance().navigate(initLoad.getGoTo());
129
                            }
130
                        }
131
                    }
132
                });
133
    }
134
}
(6-6/22)