My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.masteringwave;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.google.wave.api.*;

@SuppressWarnings("serial")
public class WorkflowServlet extends AbstractRobotServlet {

public static final String AMOUNT_FIELD = "AMOUNT_FIELD";
public static final String NAME_FIELD = "NAME_FIELD";
public static final String APPROVE_BUTTON = "APPROVE_BUTTON";
public static final String REJECT_BUTTON = "REJECT_BUTTON";
Cache cache;
public WorkflowServlet(){
super();


try {
cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
} catch (CacheException e) {
//
}

}

@Override
public void processEvents(RobotMessageBundle bundle) {
Wavelet wavelet = bundle.getWavelet();
String creator = wavelet.getCreator();
if (bundle.wasSelfAdded()) {

Blip blip = wavelet.appendBlip();
TextView textView = blip.getDocument();
textView.appendMarkup("<p><b>Welcome to the First Wave Bank</b></p>");
textView.append("\nDescribe what you want to borrow to in the wave. \n");
textView.append("Then fill out the form for your information\n");
FormView form = textView.getFormView();
form.append(new FormElement(ElementType.LABEL, "labelname",
"Your name"));
form.append(new FormElement(ElementType.INPUT, NAME_FIELD));
textView.append("\n");
form.append(new FormElement(ElementType.LABEL, "labelamount",
"Borrow amount in $"));
form.append(new FormElement(ElementType.INPUT, AMOUNT_FIELD));
form.append(new FormElement(ElementType.BUTTON, "submit", "Apply"));

}

for (Event e : bundle.getEvents()) {
if (e.getType() == EventType.BLIP_SUBMITTED) {
// get event blip
Blip blip = e.getBlip();
TextView textView = blip.getDocument();

}
if (e.getType() == EventType.FORM_BUTTON_CLICKED) {

Blip blip = e.getBlip();

FormView form = blip.getDocument().getFormView();
FormElement name = form.getFormElement(NAME_FIELD);
FormElement amount = form.getFormElement(AMOUNT_FIELD);
FormElement reject = form.getFormElement(REJECT_BUTTON);
FormElement approve = form.getFormElement(APPROVE_BUTTON);
if (name != null) {
// we are in the application form.
String errorMessage = "";
if (name.getValue() == null
|| name.getValue().trim().length() == 0) {
errorMessage += "Please enter name:\n";
}
if (amount.getValue() == null
|| amount.getValue().trim().length() == 0) {
errorMessage += "Please enter amount:\n";
}else {
try{
BigDecimal bd = new BigDecimal(amount.getValue());
}catch(Exception bdException ){
errorMessage+="Amount: "+ amount.getValue()+" is not a number";
}

}

if (errorMessage.length() > 0) {

blip.getDocument().appendStyledText(
new StyledText("\n" + errorMessage,
StyleType.BOLD));
} else {
blip.getDocument().appendStyledText(
new StyledText("\nSubmitted for appr" +
"oval",
StyleType.BOLD));
createApproveForm(wavelet);
// save name and amount
cache.put(wavelet.getWaveId()+"name", name.getValue());
cache.put(wavelet.getWaveId()+"amount", amount.getValue());

}
}else{
// We are in the Approve/Reject tab.
// validate the the user is not trying to validate his own application
if(e.getModifiedBy().equals(wavelet.getCreator())){
blip.getDocument().appendStyledText(
new StyledText("\nYou cannot approve your own application",
StyleType.BOLD));
}else{

Blip resultBlip = wavelet.appendBlip();
// check if hte approve button is pressed.
if(approve.getValue().equalsIgnoreCase("clicked")){
String nameReturn =(String)cache.get(wavelet.getWaveId()+"name");
String amountReturn = (String) cache.get(wavelet.getWaveId()+"amount");
resultBlip.getDocument().append("Application approved. "+nameReturn+" will receive your $"+amountReturn+ " soon");
}
if(reject.getValue().equalsIgnoreCase("clicked")){
resultBlip.getDocument().append("Application rejected. ");
}
}
}

}

}

}

private void createApproveForm(Wavelet wavelet) {
Blip blip = wavelet.appendBlip();
TextView textView = blip.getDocument();
textView
.appendMarkup("<p><b>Please approve or rejct the appliction</b></p>");
textView.append("\n");
FormView form = textView.getFormView();

form.append(new FormElement(ElementType.BUTTON, REJECT_BUTTON, "Reject"));

form.append(new FormElement(ElementType.BUTTON, APPROVE_BUTTON, "Approve"));

String[] creator = wavelet.getCreator().split("@");
// ok maybe not the best way to find the approver.
String approver = creator[0] + "-test@" + creator[1];
wavelet.addParticipant(approver);
}

public List getAllAppspot(List participant) {
List appspot = new ArrayList();
Iterator iterar = participant.iterator();
while (iterar.hasNext()) {
String iter = (String) iterar.next();
int mid = iter.lastIndexOf("@");
String ext = iter.substring(mid + 1, iter.length());
if (ext.equals("appspot.com")) {
appspot.add(iter);
System.out.println(iter);
}
}
return appspot;
}

}

Change log

r3 by dgraversen on Aug 20, 2009   Diff
Initial import of workflow robot
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 5639 bytes, 170 lines
Powered by Google Project Hosting