1 | 0 | |
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 | |
package com.thindeck.dynamo; |
31 | |
|
32 | |
import com.amazonaws.services.dynamodbv2.model.Select; |
33 | |
import com.google.common.collect.Iterables; |
34 | |
import com.jcabi.aspects.Immutable; |
35 | |
import com.jcabi.dynamo.AttributeUpdates; |
36 | |
import com.jcabi.dynamo.Item; |
37 | |
import com.jcabi.dynamo.QueryValve; |
38 | |
import com.jcabi.dynamo.Region; |
39 | |
import com.jcabi.xml.StrictXML; |
40 | |
import com.jcabi.xml.XML; |
41 | |
import com.jcabi.xml.XMLDocument; |
42 | |
import com.jcabi.xml.XSL; |
43 | |
import com.jcabi.xml.XSLDocument; |
44 | |
import com.thindeck.api.Agent; |
45 | |
import com.thindeck.api.Deck; |
46 | |
import com.thindeck.api.Events; |
47 | |
import java.io.IOException; |
48 | |
import java.util.Iterator; |
49 | |
import lombok.EqualsAndHashCode; |
50 | |
import lombok.ToString; |
51 | |
import org.xembly.Directive; |
52 | |
import org.xembly.Xembler; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | @ToString |
62 | |
@Immutable |
63 | 0 | @EqualsAndHashCode(of = { "region", "user", "deck" }) |
64 | |
final class DyDeck implements Deck { |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public static final String TBL = "decks"; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public static final String HASH = "user"; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public static final String RANGE = "name"; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public static final String ATTR_UPDATED = "updated"; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public static final String ATTR_MEMO = "deck"; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 0 | private static final XSL CLEANUP = XSLDocument.make( |
95 | |
DyDeck.class.getResourceAsStream("cleanup.xsl") |
96 | |
); |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
private final transient Region region; |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
private final transient String user; |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
private final transient String deck; |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | 0 | DyDeck(final Region reg, final String owner, final String name) { |
120 | 0 | this.region = reg; |
121 | 0 | this.user = owner; |
122 | 0 | this.deck = name; |
123 | 0 | } |
124 | |
|
125 | |
@Override |
126 | |
public String name() { |
127 | 0 | return this.deck; |
128 | |
} |
129 | |
|
130 | |
@Override |
131 | |
public void exec(final Agent agent) throws IOException { |
132 | 0 | final String text = this.item().get(DyDeck.ATTR_MEMO).getS(); |
133 | 0 | final XML xml = new StrictXML( |
134 | |
Deck.UPGRADE.transform( |
135 | |
DyDeck.CLEANUP.transform(new XMLDocument(text)) |
136 | |
), |
137 | |
Deck.SCHEMA |
138 | |
); |
139 | 0 | final Iterable<Directive> dirs = agent.exec(xml); |
140 | 0 | if (!Iterables.isEmpty(dirs)) { |
141 | 0 | final String update = new StrictXML( |
142 | |
new XMLDocument( |
143 | |
new Xembler(dirs).applyQuietly(xml.node()) |
144 | |
), |
145 | |
Deck.SCHEMA |
146 | |
).toString(); |
147 | 0 | if (!text.equals(update)) { |
148 | 0 | this.item().put( |
149 | |
new AttributeUpdates().with( |
150 | |
DyDeck.ATTR_MEMO, |
151 | |
update |
152 | |
) |
153 | |
); |
154 | |
} |
155 | |
} |
156 | 0 | } |
157 | |
|
158 | |
@Override |
159 | |
public Events events() { |
160 | 0 | return new DyEvents(this.region, this.name()); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
private Item item() { |
168 | 0 | final Iterator<Item> items = this.region |
169 | |
.table(DyDeck.TBL) |
170 | |
.frame() |
171 | |
.through( |
172 | |
new QueryValve() |
173 | |
.withSelect(Select.ALL_ATTRIBUTES) |
174 | |
.withLimit(1) |
175 | |
) |
176 | |
.where(DyDeck.HASH, this.user) |
177 | |
.where(DyDeck.RANGE, this.deck) |
178 | |
.iterator(); |
179 | 0 | if (!items.hasNext()) { |
180 | 0 | throw new IllegalArgumentException( |
181 | |
String.format("deck '%s' not found", this.deck) |
182 | |
); |
183 | |
} |
184 | 0 | return items.next(); |
185 | |
} |
186 | |
} |