package org.se.lab.web;

import org.apache.log4j.Logger;
import org.se.lab.db.data.Community;
import org.se.lab.service.CommunityService;
import org.se.lab.service.ServiceException;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by sattlerb on 31/10/2017.
 */
@Named
@RequestScoped
@ViewScoped
@ManagedBean(name = "AdminDataBean")
public class AdminDataBean  implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private final static Logger LOG = Logger.getLogger(AdminDataBean.class);
    private List<Community> requestedCommunityList;
    private List<Community> approvedCommunityList;
    /*
     * Properties for Session
     */
    private Flash flash;
    private FacesContext context;
    private List<Community> selectedCommunities;
    private Community selectedCommunity;
    private String id = "";
    private int userId = 0;
    private String reactionOnPendingRequest = null;
    @Inject
    private CommunityService service;

    public String getReactionOnPendingRequest() {
        return reactionOnPendingRequest;
    }

    public List<Community> getRequestedCommunityList() {
        return requestedCommunityList;
    }

    public void setRequestedCommunityList(List<Community> requestedCommunityList) {
        this.requestedCommunityList = requestedCommunityList;
    }

    public List<Community> getApprovedCommunityList() {
        return approvedCommunityList;
    }

    public void setApprovedCommunityList(List<Community> approvedCommunityList) {
        this.approvedCommunityList = approvedCommunityList;
    }

    @PostConstruct
    public void init() {
        context = FacesContext.getCurrentInstance();

        /**
         * FG Info Flash: We need flash to make the param survive one redirect request
         * otherwise param will be null
         **/
        flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
        //Get UserId of user, who owns session
        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");

            }
        }
        requestedCommunityList = new ArrayList<>();
        approvedCommunityList = new ArrayList<>();

        requestedCommunityList = service.getPending();
        LOG.info("Size of Requested Comm: " + requestedCommunityList.size());

        approvedCommunityList = service.getApproved();
        LOG.info("Size of Requested Comm: " + approvedCommunityList.size());
    }

    public void declineRequestedCommunity(Community community) {
        LOG.info("Community declined >" + community);

        try {
            service.refuse(community);
            reactionOnPendingRequest = "Sucessfully declined";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(reactionOnPendingRequest));
            refreshPage();
        } catch (ServiceException e) {
            reactionOnPendingRequest = "Decline failed";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, reactionOnPendingRequest, "Please retry"));

        }
    }

    public void approveRequestedCommunity(Community community) {
        LOG.info("Community approved > " + community);

        try {
            service.approve(community);
            refreshPage();
        } catch (ServiceException e) {
            reactionOnPendingRequest = "Approval failed";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, reactionOnPendingRequest, "Please retry"));

        }

    }

    private void refreshPage() {
        try {
            context.getExternalContext().redirect("/pse/adminPortal.xhtml");
        } catch (IOException e) {
            LOG.error("Can't redirect to /pse/adminPortal.xhtml");

        }
    }

    public Community getSelectedCommunity() {
        return selectedCommunity;
    }

    public void setSelectedCommunity(Community selectedCommunity) {
        this.selectedCommunity = selectedCommunity;
    }

    public List<Community> getSelectedCommunities() {
        return selectedCommunities;
    }

    public void setSelectedCommunities(List<Community> selectedCommunities) {
        this.selectedCommunities = selectedCommunities;
    }

    public void goToCommunity() {
        LOG.info("In Method goToCommunity");

        if (selectedCommunity != null) {

            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().getSessionMap().put("communityId", selectedCommunity.getId());

            try {
                context.getExternalContext().redirect("/pse/communityprofile.xhtml");
            } catch (IOException e) {
                LOG.error("Can't redirect to /pse/communityprofile.xhtml");
            }
        }
    }
    
    private void writeObject(ObjectOutputStream stream)
            throws IOException {
        stream.defaultWriteObject();
    }

    private void readObject(ObjectInputStream stream)
            throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
    }

}

@@ -372,32 +372,76 @@
 ean.ViewScoped;%0A
+import javax.faces.context.ExternalContext;%0A
 import javax.fac
@@ -930,31 +930,8 @@
  %7B%0A%0A
-    /**%0A     *%0A     */%0A
     
@@ -1164,52 +1164,8 @@
 st;%0A
-    /*%0A     * Properties for Session%0A     */
 %0A   
@@ -1198,21 +1198,24 @@
 private 
-Faces
+External
 Context 
@@ -1341,16 +1341,47 @@
 d = %22%22;%0A
+    private String userProfId;%0A
     priv
@@ -1452,16 +1452,21 @@
 = null;%0A
+    %0A
     @Inj
@@ -2211,162 +2211,31 @@
 ce()
-;%0A%0A        /**%0A         * FG Info Flash: We need flash to make the param survive one redirect request%0A         * otherwise param will be null%0A         **/
+.getExternalContext();%0A
 %0A   
@@ -2407,37 +2407,16 @@
 context.
-getExternalContext().
 getSessi
@@ -2421,25 +2421,24 @@
 sionMap();%0A%0A
-%0A
         id =
@@ -2446,37 +2446,16 @@
 context.
-getExternalContext().
 getReque
@@ -2518,25 +2518,24 @@
 , id);%0A%0A
-%0A
         
 String u
@@ -2526,23 +2526,16 @@
         
-String 
 userProf
@@ -2562,37 +2562,16 @@
 context.
-getExternalContext().
 getFlash
@@ -2586,17 +2586,24 @@
 uid%22));%0A
-%0A
+        
 %0A       
@@ -2865,37 +2865,16 @@
 context.
-getExternalContext().
 redirect
@@ -4576,37 +4576,16 @@
 context.
-getExternalContext().
 redirect

Refactor AdminDataBean