StocksResource.java
/**
 * Copyright 2012 the original author or authors
 *
 * 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
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.googlecode.phisix.api.resource;
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.*;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import org.apache.commons.lang3.time.DateParser;
import org.apache.commons.lang3.time.FastDateFormat;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.googlecode.phisix.api.model.Stock;
import com.googlecode.phisix.api.model.Stocks;
import com.googlecode.phisix.api.repository.StocksRepository;
import com.googlecode.phisix.api.repository.StocksRepositoryImpl;
/**
 * * Handles:
 * <ul>
 * <li>GET /stocks.xml</li>
 * <li>GET /stocks.json</li>
 * <li>GET /stocks/{symbol}.xml</li>
 * <li>GET /stocks/{symbol}.json</li>
 * <li>GET /stocks/{symbol}.{yyyy-MM-dd}.xml</li>
 * <li>GET /stocks/{symbol}.{yyyy-MM-dd}.json</li>
 * </ul>
 * 
 * @author Edge Dalmacio
 * 
 */
@Path(value = "/stocks")
public class StocksResource {
	private static final DateParser dateParser = FastDateFormat.getInstance("yyyy-MM-dd", TimeZone.getTimeZone("GMT+8"));
	private final StocksRepository stocksRepository;
	public StocksResource() throws Exception {
		this(new StocksRepositoryImpl());
	}
	
	public StocksResource(StocksRepository stocksRepository) {
		this.stocksRepository = stocksRepository;
	}
	@GET
	@Path(value = "/")
	public Stocks getAllStocks() {
		return stocksRepository.findAll();
	}
	@GET
	@Path(value = "/{symbol}")
	public Stocks getStock(@PathParam(value = "symbol") String symbol) {
		Stocks allStocks = getAllStocks();
		for (Stock stock : allStocks.getStocks()) {
			if (symbol.equalsIgnoreCase(stock.getSymbol())) {
				Stocks stocks = new Stocks();
				stocks.getStocks().add(stock);
				stocks.setAsOf(allStocks.getAsOf());
				return stocks;
			}
		}
		throw new NotFoundException();
	}
	
	public Stocks getStockByDate(@PathParam(value = "symbol") String symbol,
			@PathParam(value = "date") String date) {
		return getStockByDate(symbol, date, "PH");
	}
	
	/**
	 * 
	 * @param symbol           stock symbol
	 * @param date             trading date
	 * @param appengineCountry determines if the request is from Appengine or not
	 * @return
	 */
	@GET
	@Path(value = "/{symbol}.{date:[0-9]{4}-[0-9]{2}-[0-9]{2}}")
	public Stocks getStockByDate(@PathParam(value = "symbol") String symbol,
			@PathParam(value = "date") String date,
			@HeaderParam(value = "X-Appengine-Country") String appengineCountry) {
		if (appengineCountry == null) {
			Stocks stocks = stocksRepository.findBySymbolAndTradingDate(symbol, date);
			// Override as_of date with the date from URL parameter
			if (stocks != null) {
				Date tradingDate;
				try {
					tradingDate = dateParser.parse(date);
					Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
					calendar.setTime(tradingDate);
					calendar.set(Calendar.HOUR_OF_DAY, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
					calendar.set(Calendar.MILLISECOND, 0);
					stocks.setAsOf(calendar);
				} catch (ParseException e) {
					// If parsing fails, keep the original as_of from API response
				}
			}
			return stocks;
		} else {
			Date tradingDate;
			try {
				tradingDate = dateParser.parse(date);
			} catch (ParseException e) {
				throw new BadRequestException(e.getMessage());
			}
			return stocksRepository.findBySymbolAndTradingDate(symbol.toUpperCase(), tradingDate);
		}
	}
	@GET @POST
	@Path(value = "/archive")
	public void archive(
			@HeaderParam(value = "X-AppEngine-Cron") 
			@DefaultValue(value = "false") boolean isCron) {
		if (isCron) {
			QueueFactory.getDefaultQueue().add(withUrl("/stocks/archive"));
		} else {
			stocksRepository.save(getAllStocks());
		}
	}
}