Subversion Repository Public Repository

playgrnd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * VoltTablesToAccountConverter.java Oct 31, 2012
 *
 * Copyright 2012 Uralian, LLC. All rights reserved.
 */
package com.uralian.volt.converter;

import java.util.*;

import org.springframework.core.convert.converter.Converter;
import org.voltdb.VoltTable;

import com.uralian.volt.model.Account;
import com.uralian.volt.model.Reward;
import com.uralian.volt.model.RewardItem;

/**
 * @author Vlad Orzhekhovskiy
 */
public class VoltTablesToAccountConverter implements
    Converter<VoltTable[], Account>
{
	private static final VoltTableRowToAccountConverter vtr2acct = new VoltTableRowToAccountConverter();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.core.convert.converter.Converter#convert(java.lang.
	 * Object)
	 */
	@Override
	public Account convert(VoltTable[] source)
	{
		Account account = vtr2acct.convert(source[0].fetchRow(0));

		VoltTable rewards = source[1];
		if (rewards.getRowCount() < 1)
			return account;

		Map<Long, Reward> rewardMap = new HashMap<Long, Reward>();

		rewards.resetRowPosition();
		while (rewards.advanceRow())
		{
			long rewardId = rewards.getLong("reward_id");
			Reward reward = rewardMap.get(rewardId);
			if (reward == null)
			{
				reward = new Reward();
				reward.setRewardId(rewardId);
				reward.setAccountId(account.getAccountId());
				reward.setAmount((int) rewards.getLong("total_amount"));
				if (rewards.wasNull())
					reward.setAmount(null);
				reward.setDescription(rewards.getString("total_description"));
				reward.setCreatedOn(rewards
				    .getTimestampAsSqlTimestamp("total_created_on"));
				rewardMap.put(reward.getRewardId(), reward);
			}

			RewardItem item = new RewardItem();
			item.setItemId(rewards.getLong("item_id"));
			item.setRewardId(reward.getRewardId());
			item.setAccountId(account.getAccountId());
			item.setAmount((int) rewards.getLong("item_amount"));
			if (rewards.wasNull())
				item.setAmount(null);
			item.setDescription(rewards.getString("description"));
			item.setCreatedOn(rewards.getTimestampAsSqlTimestamp("created_on"));

			reward.getItems().add(item);
		}

		for (Reward reward : rewardMap.values())
			account.getRewards().add(reward);

		Collections.sort(account.getRewards(), new Comparator<Reward>()
		{
			@Override
			public int compare(Reward r1, Reward r2)
			{
				return r1.getCreatedOn().compareTo(r2.getCreatedOn());
			}
		});

		return account;
	}
}

Commits for playgrnd/loyalty/loyalty-core/src/main/java/com/uralian/volt/converter/VoltTablesToAccountConverter.java

Diff revisions: vs.
Revision Author Commited Message
29 snark picture snark Wed 31 Oct, 2012 16:39:11 +0000

VOLT-to-POJO converters implemented