1package eu.siacs.conversations.xmpp.forms;
2
3import android.os.Bundle;
4
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.List;
8import java.util.stream.Collectors;
9
10import eu.siacs.conversations.xml.Element;
11import eu.siacs.conversations.xml.Namespace;
12
13public class Data extends Element {
14
15 public static final String FORM_TYPE = "FORM_TYPE";
16
17 public Data() {
18 super("x");
19 this.setAttribute("xmlns", Namespace.DATA);
20 }
21
22 public List<Field> getFields() {
23 ArrayList<Field> fields = new ArrayList<Field>();
24 for(Element child : getChildren()) {
25 if (child.getName().equals("field")
26 && !FORM_TYPE.equals(child.getAttribute("var"))) {
27 fields.add(Field.parse(child));
28 }
29 }
30 return fields;
31 }
32
33 public Field getFieldByName(String needle) {
34 for(Element child : getChildren()) {
35 if (child.getName().equals("field")
36 && needle.equals(child.getAttribute("var"))) {
37 return Field.parse(child);
38 }
39 }
40 return null;
41 }
42
43 public Field put(String name, String value) {
44 Field field = getFieldByName(name);
45 if (field == null) {
46 field = new Field(name);
47 this.addChild(field);
48 }
49 field.setValue(value);
50 return field;
51 }
52
53 public void put(String name, Collection<String> values) {
54 Field field = getFieldByName(name);
55 if (field == null) {
56 field = new Field(name);
57 this.addChild(field);
58 }
59 field.setValues(values);
60 }
61
62 public void submit(Bundle options) {
63 for (Field field : getFields()) {
64 if (options.containsKey(field.getFieldName())) {
65 field.setValue(options.getString(field.getFieldName()));
66 }
67 }
68 submit();
69 }
70
71 public void submit() {
72 this.setAttribute("type","submit");
73 removeUnnecessaryChildren();
74 for(Field field : getFields()) {
75 field.removeNonValueChildren();
76 }
77 }
78
79 private void removeUnnecessaryChildren() {
80 setChildren(getChildren().stream().filter(element -> element.getName().equals("field") || element.getName().equals("title")).collect(Collectors.toList()));
81 }
82
83 public static Data parse(Element element) {
84 Data data = new Data();
85 data.setAttributes(element.getAttributes());
86 data.setChildren(element.getChildren());
87 return data;
88 }
89
90 public void setFormType(String formType) {
91 Field field = this.put(FORM_TYPE, formType);
92 field.setAttribute("type","hidden");
93 }
94
95 public String getFormType() {
96 String type = getValue(FORM_TYPE);
97 return type == null ? "" : type;
98 }
99
100 public String getValue(String name) {
101 Field field = this.getFieldByName(name);
102 return field == null ? null : field.getValue();
103 }
104
105 public String getTitle() {
106 return findChildContent("title");
107 }
108
109
110 public static Data create(String type, Bundle bundle) {
111 Data data = new Data();
112 data.setFormType(type);
113 data.setAttribute("type","submit");
114 for(String key : bundle.keySet()) {
115 data.put(key,bundle.getString(key));
116 }
117 return data;
118 }
119
120}