<commit_before>/**
 * Copyright 2017 <NAME>
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.cph.stock.web.servlet.share;

import fr.cph.stock.business.UserBusiness;
import fr.cph.stock.entities.Portfolio;
import fr.cph.stock.entities.User;
import fr.cph.stock.exception.NotFoundException;
import fr.cph.stock.exception.YahooException;
import fr.cph.stock.guice.GuiceInjector;
import fr.cph.stock.language.LanguageFactory;
import fr.cph.stock.util.Info;
import fr.cph.stock.web.servlet.CookieManagement;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

import static fr.cph.stock.util.Constants.*;

/**
 * This servlet is called when the user want to access to the history page
 *
 * @author <NAME>
 */
@Log4j2
@WebServlet(name = "ShareValueServlet", urlPatterns = {"/sharevalue"})
public class ShareValueServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private UserBusiness userBusiness;
	private LanguageFactory language;
	private static final int ITEM_MAX = 20;

	@Override
	public final void init() throws ServletException {
		userBusiness = GuiceInjector.INSTANCE.getUserBusiness();
		language = LanguageFactory.INSTANCE;
	}

	@Override
	protected final void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
		try {
			final HttpSession session = request.getSession(false);
			final User user = (User) session.getAttribute(USER);
			final String page = request.getParameter(PAGE);
			final int pageNumber = StringUtils.isEmpty(page) ? 1 : Integer.parseInt(page);
			try {
				final Portfolio portfolio = userBusiness.getUserPortfolio(user.getId()).orElseThrow(() -> new NotFoundException(user.getId()));
				if (portfolio.getShareValues().size() != 0) {
					int begin = pageNumber * ITEM_MAX - ITEM_MAX;
					int end = pageNumber * ITEM_MAX - 1;
					int nbPage = portfolio.getShareValues().size() / ITEM_MAX + 1;
					if (pageNumber == 0) {
						begin = 0;
						end = portfolio.getShareValues().size() - 1;
					}
					if (pageNumber == nbPage) {
						end = portfolio.getShareValues().size() - 1;
					}
					request.setAttribute(BEGIN, begin);
					request.setAttribute(END, end);
					request.setAttribute(PAGE, page);
					request.setAttribute(NB_PAGE, nbPage);
				}
				request.setAttribute(PORTFOLIO, portfolio);
			} catch (final YahooException e) {
				log.error(e.getMessage(), e);
				throw new ServletException("Error: " + e.getMessage(), e);
			}
			final String lang = CookieManagement.getCookieLanguage(Arrays.asList(request.getCookies()));
			request.setAttribute(LANGUAGE, language.getLanguage(lang));
			request.setAttribute(APP_TITLE, Info.NAME + " &bull; History");
			request.getRequestDispatcher("jsp/sharevalue.jsp").forward(request, response);
		} catch (final Throwable t) {
			log.error(t.getMessage(), t);
			throw new ServletException("Error: " + t.getMessage(), t);
		}
	}

	@Override
	protected final void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
		doGet(request, response);<commit_msg>Springify share value servlet<commit_after>/**
 * Copyright 2017 <NAME>
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.cph.stock.web.servlet.share;

import fr.cph.stock.business.UserBusiness;
import fr.cph.stock.entities.Portfolio;
import fr.cph.stock.entities.User;
import fr.cph.stock.exception.NotFoundException;
import fr.cph.stock.exception.YahooException;
import fr.cph.stock.language.LanguageFactory;
import fr.cph.stock.util.Info;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static fr.cph.stock.util.Constants.*;

/**
 * This servlet is called when the user want to access to the history page
 *
 * @author <NAME>
 */
@SessionAttributes(USER)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Log4j2
@Controller
public class ShareValueServlet {

	private static final int ITEM_MAX = 20;

	@NonNull
	private UserBusiness userBusiness;

	@RequestMapping(value = "/sharevalue", method = RequestMethod.GET)
	protected ModelAndView history(@RequestParam(value = PAGE, defaultValue = "1") final int pageNumber,
								   @ModelAttribute final User user,
								   @CookieValue(LANGUAGE) final String lang,
								   final HttpServletRequest request,
								   final HttpServletResponse response) throws ServletException {
		final ModelAndView model = new ModelAndView("sharevalue");
		try {
			final Portfolio portfolio = userBusiness.getUserPortfolio(user.getId()).orElseThrow(() -> new NotFoundException(user.getId()));
			if (portfolio.getShareValues().size() != 0) {
				int begin = pageNumber * ITEM_MAX - ITEM_MAX;
				int end = pageNumber * ITEM_MAX - 1;
				int nbPage = portfolio.getShareValues().size() / ITEM_MAX + 1;
				if (pageNumber == 0) {
					begin = 0;
					end = portfolio.getShareValues().size() - 1;
				}
				if (pageNumber == nbPage) {
					end = portfolio.getShareValues().size() - 1;
				}
				model.addObject(BEGIN, begin);
				model.addObject(END, end);
				model.addObject(PAGE, pageNumber);
				model.addObject(NB_PAGE, nbPage);
			}
			model.addObject(PORTFOLIO, portfolio);
		} catch (final YahooException e) {
			log.error(e.getMessage(), e);
			throw new ServletException("Error: " + e.getMessage(), e);
		}
		model.addObject(LANGUAGE, LanguageFactory.INSTANCE.getLanguage(lang));
		model.addObject(APP_TITLE, Info.NAME + " &bull; History");
		return model;
{'fraction_non_alphanumeric': 0.110615148556284, 'fraction_numerical': 0.006695494490165993, 'mean_word_length': 4.440060698027314, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6720981', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2100, 'n_words': 572}