VotingCompoAddEntryView.java
2.91 KB
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
package fi.codecrew.moya.web.cdiview.voting;
import java.io.IOException;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.validation.constraints.NotNull;
import org.primefaces.model.UploadedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.beans.VotingBeanLocal;
import fi.codecrew.moya.model.CompoEntry;
import fi.codecrew.moya.model.CompoEntryFile;
import fi.codecrew.moya.web.cdiview.GenericCDIView;
@Named
@RequestScoped
public class VotingCompoAddEntryView extends GenericCDIView {
private static final long serialVersionUID = -93065881159476197L;
@EJB
VotingBeanLocal votingBean;
@NotNull
private String name;
private String notes;
private String screenMessage;
@NotNull
private UploadedFile uploadedFile;
private Integer compoId;
private String compoName;
private static final Logger logger = LoggerFactory.getLogger(VotingCompoAddEntryView.class);
public Integer getCompoId() {
return compoId;
}
public void setCompoId(Integer compoId) {
this.compoId = compoId;
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
public String getScreenMessage() {
return screenMessage;
}
public void setScreenMessage(String screenMessage) {
this.screenMessage = screenMessage;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompoName() {
return compoName;
}
public void setCompoName(String compoName) {
this.compoName = compoName;
}
public String send() {
UploadedFile file = getUploadedFile();
if (file == null)
{
super.addFaceMessage("compo.fileuploadFailed");
return null;
}
byte[] contents = null;
if (file.getContents() != null) {
contents = file.getContents();
} else {
contents = new byte[(int) file.getSize()];
try {
file.getInputstream().read(contents);
} catch (IOException e) {
logger.warn("Error reading file from stream", e);
contents = null;
}
}
if (contents == null)
{
super.addFaceMessage("compo.fileuploadFailed");
return null;
}
CompoEntry compoEntry = new CompoEntry();
compoEntry.setTitle(name);
compoEntry.setNotes(notes);
compoEntry.setScreenMessage(screenMessage);
compoEntry.setCompo(votingBean.getCompoById(compoId));
CompoEntryFile cef = new CompoEntryFile(compoEntry);
cef.setFileData(contents);
cef.setFileName(uploadedFile.getFileName());
votingBean.addEntry(compoEntry, cef);
return null;
}
public void initView() {
compoName = votingBean.getCompoById(compoId).getName();
}
}