
package org.se.lab.web;

import org.apache.log4j.Logger;
import org.se.lab.db.data.UserProfile;
import org.se.lab.service.UserService;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Named
@RequestScoped
public class UserOverviewBean {

    private final static Logger LOG = Logger.getLogger(UserOverviewBean.class);

    @Inject
    private UserService service;
    /*
     * Properties for Session
     */
    private Flash flash;
    private FacesContext context;
    private List<UserProfile> profiles;
    private UserProfile selectedProfile;
    private String id = "";
    private int userId = 0;


    @PostConstruct
    public void init() {

        //TODO Check if Session exists
        context = FacesContext.getCurrentInstance();

        flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

        Map<String, Object> session = context.getExternalContext().getSessionMap();


        id = context.getExternalContext().getRequestParameterMap().get("userid");

        flash.put("uid", id);


        String userProfId = String.valueOf(context.getExternalContext().getFlash().get("uid"));


        LOG.info("userProfId: " + userProfId);

        if (session.size() != 0 && session.get("user") != null) {

            userId = (int) session.get("user");
            LOG.info("SESSIOn UID: " + userId);
        } else {
            try {
                context.getExternalContext().redirect("/pse/index.xhtml");
            } catch (IOException e) {
                LOG.error("Can't redirect to /pse/index.xhtml");
                //e.printStackTrace();
            }
        }

        profiles = service.getAllUserProfiles();


    }

    public List<UserProfile> getProfiles() {
        return profiles;
    }

    public void setProfiles(List<UserProfile> profiles) {
        this.profiles = profiles;
    }

    public UserProfile getSelectedProfile() {
        return selectedProfile;
    }

    public void setSelectedProfile(UserProfile selectedProfile) {
        this.selectedProfile = selectedProfile;
    }

}

@@ -212,24 +212,68 @@
 uestScoped;%0A
+import javax.faces.context.ExternalContext;%0A
 import javax
@@ -727,21 +727,24 @@
 private 
-Faces
+External
 Context 
@@ -861,16 +861,47 @@
 d = %22%22;%0A
+    private String userProfId;%0A
     priv
@@ -1057,16 +1057,37 @@
 stance()
+.getExternalContext()
 ;%0A%0A     
@@ -1101,63 +1101,16 @@
 h = 
-FacesC
+c
 ontext.
-getCurrentInstance().getExternalContext().
 getF
@@ -1164,37 +1164,16 @@
 context.
-getExternalContext().
 getSessi
@@ -1178,25 +1178,24 @@
 sionMap();%0A%0A
-%0A
         id =
@@ -1203,37 +1203,16 @@
 context.
-getExternalContext().
 getReque
@@ -1274,26 +1274,24 @@
 %22, id);%0A
-%0A%0A
         
 String u
@@ -1282,23 +1282,16 @@
         
-String 
 userProf
@@ -1318,37 +1318,16 @@
 context.
-getExternalContext().
 getFlash
@@ -1339,25 +1339,24 @@
 t(%22uid%22));%0A%0A
-%0A
         LOG.
@@ -1617,29 +1617,8 @@
 ext.
-getExternalContext().
 redi
@@ -1805,24 +1805,29 @@
 %7D%0A        %7D%0A
+try %7B
 %0A        pro
@@ -1864,16 +1864,96 @@
 iles();%0A
+    %7D catch (Exception e) %7B%0A        LOG.error(%22Error in UserOverview%22, e);%0A    %7D
 %0A%0A    %7D%0A

Refactor UserOverviewBean - Add Error-Handling