com.nkapps.billing.services.SearchServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.nkapps.billing.services.SearchServiceImpl.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 com.nkapps.billing.services;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

/**
 *
 * @author nuraddin
 */
@Service("searchService")
public class SearchServiceImpl implements SearchService {

    @Override
    public String execSearchBy(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cookie sbtCookie = null;

        String searchBy = request.getParameter("searchBy");
        if (searchBy == null) {
            Cookie[] requestCookies = request.getCookies();
            for (Cookie c : requestCookies) {
                if (c.getName().equals("searchBy")) {
                    sbtCookie = c;
                }
            }
            if (sbtCookie != null) {
                searchBy = URLDecoder.decode(sbtCookie.getValue(), "UTF-8");
            } else {
                searchBy = "";
            }
        } else {
            sbtCookie = new Cookie("searchBy", URLEncoder.encode(searchBy, "UTF-8"));
            sbtCookie.setPath("/");
            response.addCookie(sbtCookie);
        }
        return searchBy;
    }

    @Override
    public String execSearchWithinDate(HttpServletRequest request, HttpServletResponse response) {
        Cookie sbtCookie = null;

        String searchWithinDate = request.getParameter("searchWithinDate");
        if (searchWithinDate == null) {
            Cookie[] requestCookies = request.getCookies();
            for (Cookie c : requestCookies) {
                if (c.getName().equals("searchWithinDate")) {
                    sbtCookie = c;
                }
            }
            if (sbtCookie != null) {
                searchWithinDate = sbtCookie.getValue();
            } else {
                searchWithinDate = "true";
            }
        } else {
            sbtCookie = new Cookie("searchWithinDate", searchWithinDate);
            sbtCookie.setPath("/");
            response.addCookie(sbtCookie);
        }
        return searchWithinDate;
    }

    @Override
    public String execSearchByDate(HttpServletRequest request, HttpServletResponse response) {
        Cookie sbdCookie = null;

        String searchByDate = request.getParameter("searchByDate");
        if (searchByDate == null) {
            Cookie[] requestCookies = request.getCookies();
            for (Cookie c : requestCookies) {
                if (c.getName().equals("searchByDate")) {
                    sbdCookie = c;
                }
            }
            if (sbdCookie != null) {
                searchByDate = sbdCookie.getValue();
            } else {
                searchByDate = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime());
            }
        } else {
            sbdCookie = new Cookie("searchByDate", searchByDate);
            sbdCookie.setPath("/");
            response.addCookie(sbdCookie);
        }
        return searchByDate;
    }
}