tradetools.dataloader.service.impl.yahoofinance.YahooFinanceStockLoaderServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for tradetools.dataloader.service.impl.yahoofinance.YahooFinanceStockLoaderServiceImpl.java

Source

/*
 * Copyright (C) 2014 Philipp Nanz
 *
 * 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 tradetools.dataloader.service.impl.yahoofinance;

import static tradetools.dataloader.service.impl.yahoofinance.YahooFinanceDataloaderConstants.COMPONENTS_TABLE;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.yql4j.YqlClient;
import org.yql4j.YqlQueryBuilder;
import org.yql4j.YqlResult;
import org.yql4j.types.QueryResultType;

import tradetools.dataloader.service.impl.AbstractStockLoaderServiceImpl;
import tradetools.persistence.domain.Stock;
import tradetools.persistence.repository.StockRepository;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;

/**
 * @author Philipp Nanz
 */
public class YahooFinanceStockLoaderServiceImpl extends AbstractStockLoaderServiceImpl {

    @Autowired
    private StockRepository stockRepository;
    @Autowired
    private YqlClient yqlClient;

    /**
     * @return the stockRepository
     */
    protected StockRepository getStockRepository() {
        return stockRepository;
    }

    /**
     * @return the yqlClient
     */
    protected YqlClient getYqlClient() {
        return yqlClient;
    }

    /* (non-Javadoc)
     * @see tradetools.dataloader.service.StockLoaderService#loadStocksBySymbolIn(java.util.List)
     */
    @Override
    public List<Stock> loadStocksBySymbolIn(List<String> symbols) {
        List<List<String>> pages = Lists.partition(symbols, 50);
        for (List<String> page : pages) {

        }
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see tradetools.dataloader.service.impl.AbstractStockLoaderServiceImpl#loadStockSymbolsByIndex(java.lang.String)
     */
    @Override
    protected List<String> loadStockSymbolsByIndex(String index) {
        List<String> ret = new ArrayList<>();
        int offset = 0;
        int limit = 50;
        YqlQueryBuilder builder = YqlQueryBuilder
                .fromQueryString("select * from mytable(@offset, @limit) where symbol=@symbol");
        builder.withTable(COMPONENTS_TABLE, "mytable");
        builder.withVariable("symbol", index);
        builder.withVariable("limit", Integer.toString(limit));
        try {
            do {
                builder.withVariable("offset", Integer.toString(offset));
                YqlResult result = getYqlClient().query(builder.build());
                List<String> page = result
                        .getContentAsMappedObject(new TypeReference<QueryResultType<List<String>>>() {
                        }).getResults();

                if ((page == null) || page.isEmpty()) {
                    break;
                }

                ret.addAll(page);
                offset += limit;
            } while (true);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
        return ret;
    }
}