Commands.AddShoesCommand.java Source code

Java tutorial

Introduction

Here is the source code for Commands.AddShoesCommand.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Commands;

import DAO.ColorDao;
import DAO.ShoesDao;
import DTO.Shoes;
import DTO.User;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 *
 * @author USER
 */
public class AddShoesCommand implements Command {

    @Override
    public String executeCommand(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String forwardToJsp = "";
        HttpSession session = request.getSession(true);
        ShoesDao sd = new ShoesDao();
        boolean check = true;
        ArrayList<String> addList = new ArrayList<>();
        ArrayList<Shoes> list = new ArrayList<>();
        HashMap<Integer, LinkedList<String>> status = new HashMap<>();
        HashMap<Integer, LinkedList<Integer>> color = new HashMap<>();
        LinkedList<String> s = null;
        LinkedList<Integer> c = null;
        if (session.getAttribute("userLogin") != null && ((User) session.getAttribute("userLogin")).is_Admin()) {
            if (request.getParameter("number") != null) {
                int num = Integer.parseInt(request.getParameter("number"));
                for (int n = 1; n < num; n++) {
                    s = new LinkedList<>();
                    c = new LinkedList<>();
                    boolean check1 = true;
                    if (request.getParameter("name-" + n) != null && request.getParameter("brand-" + n) != null
                            && request.getParameter("sport-" + n) != null) {
                        if (request.getParameter("name-" + n).isEmpty()) {
                            session.setAttribute("errorMsg", "The name cannot be empty");
                            return forwardToJsp = "AddShoes.jsp";
                        }
                        String name = (String) request.getParameter("name-" + n).substring(0, 1).toUpperCase()
                                + request.getParameter("name-" + n).substring(1).toLowerCase();
                        int brand = Integer.parseInt(request.getParameter("brand-" + n));
                        int sport = Integer.parseInt(request.getParameter("sport-" + n));
                        if (request.getParameter("price-" + n).isEmpty()) {
                            s.add("price cannot be empty");
                        }
                        double price = Double.parseDouble(request.getParameter("price-" + n));
                        if (price < 1 || price > 200) {
                            s.add("price range is between 1 to 200");
                        }
                        if (!sd.findShoes(name).isEmpty()) {
                            s.add("The name is repeated");
                        }

                        boolean repeat = false;
                        for (int i = 1; i < 4; i++) {
                            if (request.getParameter("color" + i + "-" + n) != null) {
                                int id = Integer.parseInt(request.getParameter("color" + i + "-" + n));
                                if (c.contains(id)) {
                                    repeat = true;
                                }
                                c.add(id);
                            }
                        }
                        if (repeat) {
                            s.add("The color is repeated");
                        }

                        //                        String[] files1 = request.getParameterValues("file1-" + n);
                        //                        String[] files2 = request.getParameterValues("file2-" + n);
                        //                        String[] files3 = request.getParameterValues("file3-" + n);
                        //                        long a=Arrays.stream(files1).filter((String st) -> !st.isEmpty()).count();
                        //                        long b=Arrays.stream(files1).filter((String st) -> !st.isEmpty()).count();
                        //                        long d=Arrays.stream(files1).filter((String st) -> !st.isEmpty()).count();
                        //                        if(a==0 || b==0 || d==0){
                        //                            s.add("Images is not uploaded");
                        //                        }
                        //                        p.add(files1);
                        //                        p.add(files2);
                        //                        p.add(files3);
                        if (!s.isEmpty()) {
                            status.put(n, s);
                        }
                        color.put(n, c);
                        list.add(new Shoes(n, brand, 0, sport, name, price, ""));
                    } else {
                        check = false;
                        break;
                    }
                }
                ColorDao cd = new ColorDao();

                response.setContentType("text/html");
                session.setAttribute("list", list);
                session.setAttribute("status", status);
                session.setAttribute("allcolor", color);
                if (status.isEmpty() && check) {
                    for (int i = 0; i < list.size(); i++) {
                        c = color.get(i + 1);
                        Iterator<Integer> iter = c.iterator();
                        int count = 1;

                        while (iter.hasNext()) {
                            String name = list.get(i).getName();
                            int colorId = iter.next();
                            String colorName = cd.findColorById(colorId).getColor_Name();
                            String pic = name + "-" + colorName + "-";
                            sd.addShoes(list.get(i).getBrandID(), colorId, list.get(i).getTypeID(), name,
                                    list.get(i).getPrice(), pic);
                            String colo = request.getParameter("cr" + count + "-" + (i + 1));
                            String[] col = colo.split(",");
                            String UPLOAD_DIRECTORY = request.getServletContext().getRealPath("") + File.separator
                                    + "img" + File.separator;
                            int count1 = 1;
                            for (String str : col) {
                                File file = new File(UPLOAD_DIRECTORY + str.substring(4));
                                File f = new File(UPLOAD_DIRECTORY + pic + count1 + ".jpg");
                                try {
                                    boolean check1 = file.renameTo(f);
                                    if (check1 == false) {
                                        session.setAttribute("errorMsg",
                                                str.substring(4) + " " + UPLOAD_DIRECTORY + pic);
                                        return "AddShoes.jsp";
                                    }
                                } catch (SecurityException | NullPointerException se) {
                                    session.setAttribute("errorMsg", Arrays.toString(se.getStackTrace()));
                                    return "AddShoes.jsp";

                                }
                                count1++;
                            }
                            count++;

                        }
                    }
                    session.setAttribute("errorMsg", "Shoes is successful added");
                    //                    session.removeAttribute("list");
                    //                    session.removeAttribute("allcolor");
                    //                    session.removeAttribute("status");

                } else {
                    session.setAttribute("errorMsg", "Please fill the form with correct information");
                    forwardToJsp = "AddShoes.jsp";
                }

            } else {
                session.setAttribute("errorMsg", "Fail to save changes, please refresh the page and try again");
                forwardToJsp = "shoes.jsp";
            }
        } else {
            session.setAttribute("errorMsg", "You are not allowed to access this page");
            forwardToJsp = "index.jsp";
        }
        return forwardToJsp;
    }
}