Java Code Examples for org.springframework.social.connect.ConnectionRepository#getPrimaryConnection()

The following examples show how to use org.springframework.social.connect.ConnectionRepository#getPrimaryConnection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CurrencyExchangeServiceOfflineImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 6 votes vote down vote up
private void updateCurrencyExchangeFromYahoo(String ticker) {
	String guid = AuthenticationUtil.getPrincipal().getUsername();
	String token = usersConnectionRepository.getRegisteredSocialUser(guid).getAccessToken();
	ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(guid);
	Connection<Yahoo2> connection = connectionRepository.getPrimaryConnection(Yahoo2.class);

       if (connection == null) {
       	return;
       }

	List<YahooQuote> yahooQuotes = connection.getApi().financialOperations().getYahooQuotes(Lists.newArrayList(ticker), token);
	if(yahooQuotes == null || yahooQuotes.size() > 1){
		throw new IllegalArgumentException("Currency ticker:"+ticker+" not found at Yahoo!");
	}
	currencyExchangeRepository.save(yahooCurrencyConverter.convert(yahooQuotes.get(0)));
}
 
Example 2
Source File: StockProductServiceOfflineImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
private void updateChartStockFromYahoo(StockProduct stock, ChartType type, ChartHistoSize histoSize, ChartHistoMovingAverage histoAverage,
		ChartHistoTimeSpan histoPeriod, Integer intradayWidth, Integer intradayHeight) {
	
	Preconditions.checkNotNull(stock, "stock must not be null!");
	Preconditions.checkNotNull(type, "ChartType must not be null!");
	
	String guid = AuthenticationUtil.getPrincipal().getUsername();
	String token = usersConnectionRepository.getRegisteredSocialUser(guid).getAccessToken();
	ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(guid);
	Connection<Yahoo2> connection = connectionRepository.getPrimaryConnection(Yahoo2.class);
	
       if (connection != null) {
		byte[] yahooChart = connection.getApi().financialOperations().getYahooChart(stock.getId(), type, histoSize, histoAverage, histoPeriod, intradayWidth, intradayHeight, token);
		
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm");
		LocalDateTime dateTime = LocalDateTime.now();
		String formattedDateTime = dateTime.format(formatter); // "1986-04-08_1230"
		String imageName = stock.getId().toLowerCase()+"_"+type.name().toLowerCase()+"_"+formattedDateTime+".png";
    	String pathToYahooPicture = env.getProperty("pictures.yahoo.path").concat(File.separator+imageName);
    	
           try {
               Path newPath = Paths.get(pathToYahooPicture);
               Files.write(newPath, yahooChart, StandardOpenOption.CREATE);
           } catch (IOException e) {
               throw new Error("Storage of " + pathToYahooPicture+ " failed", e);
           }
           
           ChartStock chartStock = new ChartStock(stock, type, histoSize, histoAverage, histoPeriod, intradayWidth, intradayHeight, pathToYahooPicture);
           chartStockRepository.save(chartStock);
       }
}
 
Example 3
Source File: IndexServiceOfflineImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
private void updateIndexAndQuotesFromYahoo(Set<Index> askedContent) {
	
	Set<Index> recentlyUpdated = askedContent.stream()
				.filter(t -> t.getLastUpdate() != null && DateUtil.isRecent(t.getLastUpdate(), 1))
				.collect(Collectors.toSet());
	
	if(askedContent.size() != recentlyUpdated.size()){
		
		String guid = AuthenticationUtil.getPrincipal().getUsername();
		String token = usersConnectionRepository.getRegisteredSocialUser(guid).getAccessToken();
		ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(guid);
		Connection<Yahoo2> connection = connectionRepository.getPrimaryConnection(Yahoo2.class);

        if (connection != null) {
			askedContent.removeAll(recentlyUpdated);
		
			List<String> updatableTickers = askedContent.stream()
					.map(Index::getId)
					.collect(Collectors.toList());
			
			List<YahooQuote> yahooQuotes = connection.getApi().financialOperations().getYahooQuotes(updatableTickers, token);

			Set<Index> upToDateIndex = yahooQuotes.stream()
					.map(t -> yahooIndexConverter.convert(t))
					.collect(Collectors.toSet());
			
			final Map<String, Index> persistedStocks = indexRepository.save(upToDateIndex).stream()
					.collect(Collectors.toMap(Index::getId, Function.identity()));

			Set<IndexQuote> updatableQuotes = yahooQuotes.stream()
					.map(sq -> new IndexQuote(sq, persistedStocks.get(sq.getId())))
					.collect(Collectors.toSet());

			indexQuoteRepository.save(updatableQuotes);
        }
	}
}
 
Example 4
Source File: IndexServiceOfflineImpl.java    From cloudstreetmarket.com with GNU General Public License v3.0 5 votes vote down vote up
private void updateChartIndexFromYahoo(Index index, ChartType type, ChartHistoSize histoSize, ChartHistoMovingAverage histoAverage,
		ChartHistoTimeSpan histoPeriod, Integer intradayWidth, Integer intradayHeight) {
	
	Preconditions.checkNotNull(index, "index must not be null!");
	Preconditions.checkNotNull(type, "ChartType must not be null!");
	
	String guid = AuthenticationUtil.getPrincipal().getUsername();
	String token = usersConnectionRepository.getRegisteredSocialUser(guid).getAccessToken();
	ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(guid);
       Connection<Yahoo2> connection = connectionRepository.getPrimaryConnection(Yahoo2.class);

       if (connection != null) {
		byte[] yahooChart = connection.getApi().financialOperations().getYahooChart(index.getId(), type, histoSize, histoAverage, histoPeriod, intradayWidth, intradayHeight, token);
		
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HHmm");
		LocalDateTime dateTime = LocalDateTime.now();
		String formattedDateTime = dateTime.format(formatter); // "1986-04-08_1230"
		String imageName = index.getId().toLowerCase()+"_"+type.name().toLowerCase()+"_"+formattedDateTime+".png";
    	String pathToYahooPicture = env.getProperty("user.home").concat(env.getProperty("pictures.yahoo.path")).concat(File.separator+imageName);
    	
           try {
               Path newPath = Paths.get(pathToYahooPicture);
               Files.write(newPath, yahooChart, StandardOpenOption.CREATE);
           } catch (IOException e) {
               throw new Error("Storage of " + pathToYahooPicture+ " failed", e);
           }
           
           ChartIndex chartIndex = new ChartIndex(index, type, histoSize, histoAverage, histoPeriod, intradayWidth, intradayHeight, pathToYahooPicture);
           chartIndexRepository.save(chartIndex);
       }
}