}
}
@Override
public void decode(FacesContext ctx, UIComponent component) {
FormBuilderInternal formBuilder = (FormBuilderInternal) component;
formBuilder.setInvokeCallback(true);
String formContentString = ctx.getExternalContext().getRequestParameterMap().get(getFormContentStringId(component));
String formActionString = ctx.getExternalContext().getRequestParameterMap().get(getFormActionStringId(component));
formBuilder.setActiveTab(ctx.getExternalContext().getRequestParameterMap().get(getFormActiveTabStringId(component)));
if (formActionString != null && !"".equals(formActionString)) {
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
switch (formActionString) {
case "update":
String[] contents = formContentString.split(Constants.STRINGSEPERATOR);
List<FormBuilderItemBase> list = new ArrayList<>();
Map<String, FormBuilderItemBase> cachedObjects = new HashMap<>();
String activeFormatArea = null;
String activeConstraint = null;
int positionOffset = 0;
for (String content : contents) {
if (content != null && !"".equals(content.trim())) {
FormBuilderItemBaseHelper item = null;
try {
item = mapper.readValue(content, FormBuilderItemBaseHelper.class);
} catch (IOException ex) {
Logger.getLogger(FormBuilderInternalRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
if (item != null) {
Class<FormBuilderItemBase> cls = null;
try {
cls = (Class<FormBuilderItemBase>) Class.forName(item.getClassname());
} catch (ClassNotFoundException ex) {
Logger.getLogger(FormBuilderInternalRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
Object o = null;
if (cls != null) {
try {
o = cls.cast(mapper.readValue(content, cls));
} catch (IOException ex) {
Logger.getLogger(FormBuilderInternalRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (o != null) {
//Start image specific code
if (o instanceof FormBuilderItemImage || o instanceof FormBuilderItemDownload) {
FormBuilderItemBase object = (FormBuilderItemBase) o;
for (FormBuilderItemBase tmpItem : formBuilder.getForm().getItems()) {
if (tmpItem.getId().equals(object.getId())) {
cachedObjects.put(tmpItem.getId(), tmpItem);
}
}
}
//End image specific code
FormBuilderItemBase itemBase = (FormBuilderItemBase) o;
itemBase.setPosition(itemBase.getPosition() + positionOffset);
itemBase.initConstraintsFromConstraintsString(formBuilder.getWorkflowStates(), formBuilder.getConstraintClients());
list.add((FormBuilderItemBase) o);
//Start format area specific code
if (o instanceof FormBuilderItemFormatArea || o instanceof FormBuilderItemConstraint) {
FormBuilderItemBase area = (FormBuilderItemBase) o;
if (area.getProperties().getBrother() == null) {
HashMap<String, String> areasMap = new HashMap<>();
HashMap<String, String> constraintsMap = new HashMap<>();
for (FormBuilderItemBase tmpItem : formBuilder.getForm().getItems()) {
if (tmpItem instanceof FormBuilderItemFormatArea
&& !areasMap.containsKey(tmpItem.getProperties().getFormatareauuid())) {
areasMap.put(tmpItem.getProperties().getFormatareauuid(), null);
}
if (tmpItem instanceof FormBuilderItemConstraint
&& !areasMap.containsKey(tmpItem.getProperties().getFormatareauuid())) {
constraintsMap.put(tmpItem.getProperties().getFormatareauuid(), null);
}
}
int areas = 1 + areasMap.size();
int constrains = 1 + constraintsMap.size();
FormBuilderItemBase brother = null;
if (o instanceof FormBuilderItemFormatArea) {
brother = new FormBuilderItemFormatArea();
} else {
brother = new FormBuilderItemConstraint();
}
brother.getProperties().setBrother(area.getId());
brother.setBrother(area);
brother.setPosition(area.getPosition() + 1);
area.getProperties().setBrother(brother.getId());
area.setBrother(brother);
list.add(brother);
String formatAreaUuid = UUID.randomUUID().toString();
area.getProperties().setFormatareauuid(formatAreaUuid);
brother.getProperties().setFormatareauuid(formatAreaUuid);
if (o instanceof FormBuilderItemFormatArea) {
area.getProperties().setValues(area.getProperties().getValues() + " " + areas);
brother.getProperties().setValues(brother.getProperties().getValues() + " " + areas);
} else {
area.getProperties().setValues(area.getProperties().getValues() + " " + constrains);
brother.getProperties().setValues(brother.getProperties().getValues() + " " + constrains);
}
positionOffset++;
}
if (o instanceof FormBuilderItemFormatArea) {
if (activeFormatArea == null) {
activeFormatArea = area.getProperties().getFormatareauuid();
} else if (activeFormatArea.equals(area.getProperties().getFormatareauuid())) {
activeFormatArea = null;
} else {
throw new RuntimeException(Messages.getStringJSF("formatarea.error"));
}
} else {
if (activeConstraint == null) {
activeConstraint = area.getProperties().getFormatareauuid();
} else if (activeConstraint.equals(area.getProperties().getFormatareauuid())) {
activeConstraint = null;
} else {
throw new RuntimeException(Messages.getStringJSF("constraint.error"));
}
}
}
//End format area specific code
}
}
}
}
if (!list.isEmpty()) {
formBuilder.getForm().getItems().clear();
formBuilder.getForm().addItems(list);
}
//Start image specific code
if (!cachedObjects.isEmpty()) {
for (FormBuilderItemBase tmpItem : formBuilder.getForm().getItems()) {
if ((tmpItem instanceof FormBuilderItemImage || tmpItem instanceof FormBuilderItemDownload)
&& cachedObjects.containsKey(tmpItem.getId())) {
tmpItem.getProperties().setFile(cachedObjects.get(tmpItem.getId()).getProperties().getFile());
}
}
}
HttpServletRequest request = (HttpServletRequest) ctx.getExternalContext().getRequest();
MultipartRequest multiRequest = new MultipartRequest(request);
for (String key : multiRequest.getFiles().keySet()) {
for (FormBuilderItemBase item : formBuilder.getForm().getItems()) {
if (key.contains(item.getId())) {
item.getProperties().setFile(multiRequest.getFiles().get(key));
}
}
}
//End image specific code
break;
case "edit":
try {
FormBuilderItemUpdateHolder updateHolder = mapper.readValue(formContentString, FormBuilderItemUpdateHolder.class);
List<FormBuilderItemBase> items = formBuilder.getForm().getItems();
for (FormBuilderItemBase item : items) {
if (item.getId().equals(updateHolder.getItemId())) {
FormBuilderItemFactory.updateFormBuilderItem(item, updateHolder.getUpdates());
if (item instanceof FormBuilderItemFormatArea || item instanceof FormBuilderItemConstraint) {
for (FormBuilderItemBase item2 : items) {
if (item2.getProperties().getBrother() != null
&& (item2 instanceof FormBuilderItemFormatArea || item2 instanceof FormBuilderItemConstraint)
&& item2.getProperties().getBrother().equals(item.getId())) {
Iterator<FormBuilderItemUpdate> updateIter = updateHolder.getUpdates().iterator();
while (updateIter.hasNext()) {
FormBuilderItemUpdate update = updateIter.next();
if (update.getMethod().equals("brother")) {
updateIter.remove();
}
}
FormBuilderItemFactory.updateFormBuilderItem(item2, updateHolder.getUpdates());
}
}
}
}
}
} catch (IOException ex) {
Logger.getLogger(FormBuilderInternalRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case "delete":
List<FormBuilderItemBase> items = formBuilder.getForm().getItems();
Iterator<FormBuilderItemBase> itemIter = items.iterator();
String brotherToDelete = null;
while (itemIter.hasNext()) {
FormBuilderItemBase item = itemIter.next();
if (item.getId().equals(formContentString)) {
if (item instanceof FormBuilderItemFormatArea || item instanceof FormBuilderItemConstraint) {
brotherToDelete = item.getProperties().getBrother();
}
itemIter.remove();
break;
}
}
if (brotherToDelete != null) {
itemIter = items.iterator();
while (itemIter.hasNext()) {
FormBuilderItemBase item = itemIter.next();
if (item.getId().equals(brotherToDelete)) {
itemIter.remove();
break;
}
}
}
break;
case "addconstraint":
try {
FormBuilderItemAddConstraint addConstraint = mapper.readValue(formContentString, FormBuilderItemAddConstraint.class);
for (FormBuilderItemBase tmpItem : formBuilder.getForm().getItems()) {
if (tmpItem.getId().equals(addConstraint.getItemUuid())) {
WorkflowState workflowState = null;
ConstraintClient constraintClient = null;
ConstraintType constraintType = null;
if (formBuilder.getWorkflowStates() != null) {
for (WorkflowState tmpWorkflowState : formBuilder.getWorkflowStates()) {
if (tmpWorkflowState.getUuid().equals(addConstraint.getWorkflowState())) {
workflowState = tmpWorkflowState;
break;
}
}
}
if (formBuilder.getConstraintClients() != null) {
for (ConstraintClient tmpConstraintClient : formBuilder.getConstraintClients()) {
if (tmpConstraintClient.getUuid().equals(addConstraint.getConstraintClient())) {
constraintClient = tmpConstraintClient;
break;
}
}
}
for (ConstraintType tmpConstraintType : ConstraintType.values()) {
if (tmpConstraintType.name().equals(addConstraint.getConstraintType())) {
constraintType = tmpConstraintType;
break;
}
}
tmpItem.addConstraintClient(constraintClient, workflowState, constraintType);
tmpItem.getProperties().setMaximise(Boolean.TRUE);
break;
}
}
} catch (IOException ex) {
Logger.getLogger(FormBuilderInternalRenderer.class.getName()).log(Level.SEVERE, null, ex);
}
break;
case "deleteconstraint":
try {
FormBuilderItemDeleteConstraint deleteConstraint = mapper.readValue(formContentString, FormBuilderItemDeleteConstraint.class);
for (FormBuilderItemBase tmpItem : formBuilder.getForm().getItems()) {
if (tmpItem.getId().equals(deleteConstraint.getItemUuid())) {
boolean deleted = false;
for (Iterator<Constraint> it = tmpItem.getConstraints().iterator(); it.hasNext();) {
Constraint constraint = it.next();
if (constraint.hashCode() == Integer.parseInt(deleteConstraint.getHashCode())) {