<commit_before>/**
 * Copyright 2013 <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.business;

import fr.cph.stock.dao.*;
import fr.cph.stock.entities.*;
import fr.cph.stock.enumtype.Currency;
import fr.cph.stock.enumtype.Market;
import fr.cph.stock.exception.EquityException;
import fr.cph.stock.exception.LoginException;
import fr.cph.stock.exception.YahooException;
import fr.cph.stock.exception.YahooUnknownTickerException;
import fr.cph.stock.external.IExternalDataAccess;
import fr.cph.stock.external.YahooExternalDataAccess;
import fr.cph.stock.security.Security;
import fr.cph.stock.util.Info;
import fr.cph.stock.util.Mail;
import fr.cph.stock.util.Util;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * Business class that access database and process data
 *
 * @author <NAME>
 * @version 1
 */
public final class Business implements IBusiness {

	private static final Logger LOG = Logger.getLogger(Business.class);
	private static final Object LOCK = new Object();
	private static final MathContext MATHCONTEXT = MathContext.DECIMAL32;
	private static final int MAX_UPDATE_COMPANY = 15;
	private static final int PAUSE = 1000;
	private static final int PERCENT = 100;
	private static Business BUSINESS;

	private final IExternalDataAccess yahoo;
	private final CompanyDAO daoCompany;
	private final PortfolioDAO daoPortfolio;
	private final EquityDAO daoEquity;
	private final UserDAO daoUser;
	private final CurrencyDAO daoCurrency;
	private final ShareValueDAO daoShareValue;
	private final IndexDAO daoIndex;
	private final FollowDAO daoFollow;
	private final AccountDAO daoAccount;

	private Business() {
		this.yahoo = new YahooExternalDataAccess();
		this.daoCompany = new CompanyDAO();
		this.daoPortfolio = new PortfolioDAO();
		this.daoEquity = new EquityDAO();
		this.daoUser = new UserDAO();
		this.daoCurrency = new CurrencyDAO();
		this.daoShareValue = new ShareValueDAO();
		this.daoIndex = new IndexDAO();
		this.daoFollow = new FollowDAO();
		this.daoAccount = new AccountDAO();
	}

	/**
	 * Static singleton getter
	 *
	 * @return a IBusiness instance
	 */
	public static IBusiness getInstance() {
		if (BUSINESS == null) {
			synchronized (LOCK) {
				if (BUSINESS == null) {
					BUSINESS = new Business();
				}
			}
		}
		return BUSINESS;
	}

	@Override
	public final void createEquity(final int userId, final String ticker, final Equity equity) throws EquityException, YahooException {
		final Company company = addOrUpdateCompany(ticker);
		final Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);

		final Optional<Equity> found = portfolio.getEquities().stream()
			.filter(e -> e.getCompanyId() == company.getId())
			.findAny();
		if (found.isPresent()) {
			throw new EquityException(ticker + EquityException.ENTITY_ALREADY_RECORDED);
		} else {
			equity.setCompanyId(company.getId());
			equity.setPortfolioId(portfolio.getId());
			daoEquity.insert(equity);
		}
	}

	@Override
	public final void createManualEquity(final int userId, final Company company, final Equity equity) throws EquityException {
		Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);
		boolean isAlreadyThere = false;
		for (Equity e : portfolio.getEquities()) {
			if (e.getCompanyId() == company.getId()) {
				isAlreadyThere = true;
				equity.setid(e.getId());
			}
		}
		equity.setCompanyId(company.getId());
		equity.setPortfolioId(portfolio.getId());
		if (isAlreadyThere) {
			throw new EquityException(company.getName() + EquityException.ENTITY_ALREADY_RECORDED);
		} else {
			daoEquity.insert(equity);
		}
	}

	@Override
	public final void updateEquity(final int userId, final String ticker, final Equity equity) throws UnsupportedEncodingException, YahooException {
		Company company = daoCompany.selectWithYahooId(ticker);
		Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);

		boolean isAlreadyThere = false;
		for (Equity e : portfolio.getEquities()) {
			if (e.getCompanyId() == company.getId()) {
				isAlreadyThere = true;
				equity.setid(e.getId());
			}
		}
		equity.setCompanyId(company.getId());
		equity.setPortfolioId(portfolio.getId());
		if (isAlreadyThere) {
			daoEquity.update(equity);
		} else {
			daoEquity.insert(equity);
		}
	}

	@Override<commit_msg>Refactor create equity in business<commit_after>/**
 * Copyright 2013 <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.business;

import fr.cph.stock.dao.*;
import fr.cph.stock.entities.*;
import fr.cph.stock.enumtype.Currency;
import fr.cph.stock.enumtype.Market;
import fr.cph.stock.exception.EquityException;
import fr.cph.stock.exception.LoginException;
import fr.cph.stock.exception.YahooException;
import fr.cph.stock.exception.YahooUnknownTickerException;
import fr.cph.stock.external.IExternalDataAccess;
import fr.cph.stock.external.YahooExternalDataAccess;
import fr.cph.stock.security.Security;
import fr.cph.stock.util.Info;
import fr.cph.stock.util.Mail;
import fr.cph.stock.util.Util;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.MathContext;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * Business class that access database and process data
 *
 * @author <NAME>
 * @version 1
 */
public final class Business implements IBusiness {

	private static final Logger LOG = Logger.getLogger(Business.class);
	private static final Object LOCK = new Object();
	private static final MathContext MATHCONTEXT = MathContext.DECIMAL32;
	private static final int MAX_UPDATE_COMPANY = 15;
	private static final int PAUSE = 1000;
	private static final int PERCENT = 100;
	private static Business BUSINESS;

	private final IExternalDataAccess yahoo;
	private final CompanyDAO daoCompany;
	private final PortfolioDAO daoPortfolio;
	private final EquityDAO daoEquity;
	private final UserDAO daoUser;
	private final CurrencyDAO daoCurrency;
	private final ShareValueDAO daoShareValue;
	private final IndexDAO daoIndex;
	private final FollowDAO daoFollow;
	private final AccountDAO daoAccount;

	private Business() {
		this.yahoo = new YahooExternalDataAccess();
		this.daoCompany = new CompanyDAO();
		this.daoPortfolio = new PortfolioDAO();
		this.daoEquity = new EquityDAO();
		this.daoUser = new UserDAO();
		this.daoCurrency = new CurrencyDAO();
		this.daoShareValue = new ShareValueDAO();
		this.daoIndex = new IndexDAO();
		this.daoFollow = new FollowDAO();
		this.daoAccount = new AccountDAO();
	}

	/**
	 * Static singleton getter
	 *
	 * @return a IBusiness instance
	 */
	public static IBusiness getInstance() {
		if (BUSINESS == null) {
			synchronized (LOCK) {
				if (BUSINESS == null) {
					BUSINESS = new Business();
				}
			}
		}
		return BUSINESS;
	}

	@Override
	public final void createEquity(final int userId, final String ticker, final Equity equity) throws EquityException, YahooException {
		final Company company = addOrUpdateCompany(ticker);
		final Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);
		createEquity(portfolio, company, equity);
	}

	@Override
	public final void createManualEquity(final int userId, final Company company, final Equity equity) throws EquityException {
		final Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);
		createEquity(portfolio, company, equity);
	}

	private void createEquity(final Portfolio portfolio, final Company company, final Equity equity) throws EquityException {
		final Optional<Equity> found = portfolio.getEquities().stream()
			.filter(e -> e.getCompanyId() == company.getId())
			.findAny();
		if (found.isPresent()) {
			throw new EquityException(company.getName() + EquityException.ENTITY_ALREADY_RECORDED);
		} else {
			equity.setCompanyId(company.getId());
			equity.setPortfolioId(portfolio.getId());
			daoEquity.insert(equity);
		}
	}

	@Override
	public final void updateEquity(final int userId, final String ticker, final Equity equity) throws UnsupportedEncodingException, YahooException {
		Company company = daoCompany.selectWithYahooId(ticker);
		Portfolio portfolio = daoPortfolio.selectPortfolioFromUserIdWithEquities(userId, null, null);

		boolean isAlreadyThere = false;
		for (Equity e : portfolio.getEquities()) {
			if (e.getCompanyId() == company.getId()) {
				isAlreadyThere = true;
				equity.setid(e.getId());
			}
		}
		equity.setCompanyId(company.getId());
		equity.setPortfolioId(portfolio.getId());
		if (isAlreadyThere) {
			daoEquity.update(equity);
		} else {
			daoEquity.insert(equity);
		}
	}

	@Override
{'fraction_non_alphanumeric': 0.09952558796810336, 'fraction_numerical': 0.004239426668012516, 'mean_word_length': 4.780630105017503, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '3011915', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3051, 'n_words': 828}