r1010 - trunk/wikitty-struts/src/main/java/org/nuiton/wikitty/struts
Author: mfortun Date: 2011-06-30 15:34:18 +0200 (Thu, 30 Jun 2011) New Revision: 1010 Url: http://nuiton.org/repositories/revision/wikitty/1010 Log: * correct ordering Modified: trunk/wikitty-struts/src/main/java/org/nuiton/wikitty/struts/WikittyFieldHandler.java Modified: trunk/wikitty-struts/src/main/java/org/nuiton/wikitty/struts/WikittyFieldHandler.java =================================================================== --- trunk/wikitty-struts/src/main/java/org/nuiton/wikitty/struts/WikittyFieldHandler.java 2011-06-30 12:15:59 UTC (rev 1009) +++ trunk/wikitty-struts/src/main/java/org/nuiton/wikitty/struts/WikittyFieldHandler.java 2011-06-30 13:34:18 UTC (rev 1010) @@ -12,6 +12,7 @@ import org.apache.commons.logging.LogFactory; import org.nuiton.util.StringUtil; import org.nuiton.wikitty.WikittyProxy; +import org.nuiton.wikitty.WikittyUtil; import org.nuiton.wikitty.entities.FieldType; import org.nuiton.wikitty.entities.Wikitty; import org.nuiton.wikitty.entities.WikittyExtension; @@ -102,8 +103,18 @@ this.order = order; } + /* + * Method called two times by wikitty tag + * + */ public Collection<ExtensionFieldStrutsBean> getWikittyField() { + // if order before selected and none field add, prepare the field list + // with another method + if (orderBefore && (fieldAdded.size() == 0)) { + return getFieldOrderedBefore(); + } + Map<String, ExtensionFieldStrutsBean> mapField = new HashMap<String, ExtensionFieldStrutsBean>(); /* @@ -121,103 +132,45 @@ * if extension excluded or not include while include is enable jump * to the next extension */ - if ((excludeMap.containsKey(extName) && excludeMap.get(extName) - .contains("*")) - || (includeEnable && !includeMap.containsKey(extName))) { - log.debug("extension: " + extName + " Skipped"); - + if (!isIncluded(extName, "*")) { continue; - } for (String fieldName : ext.getFieldNames()) { - FieldType fieldType = wikitty.getExtension(extName) - .getFieldType(fieldName); + /* * if field is excluded or field not included while include mode * jump to the next field or if field allready added to the page */ - if ((excludeMap.containsKey(extName) && excludeMap.get(extName) - .contains(fieldName)) - || (includeEnable && !includeMap.get(extName).contains( - fieldName)) - || fieldAdded.contains(extName + "." + fieldName)) { - log.debug("field: " + extName + "." + fieldName - + " Skipped"); + if (!isIncluded(extName, fieldName)) { continue; - } - ExtensionFieldStrutsBean temp = new ExtensionFieldStrutsBean(); + // delegate construction of the element + ExtensionFieldStrutsBean temp = constructExtensionFieldFromField( + extName, fieldName); - temp.setName(extName + "." + fieldName); - temp.setLabel(temp.getName()); - temp.setValue(""); - - // set the field type. - switch (fieldType.getType()) { - case BINARY: - temp.setType("file"); - - break; - case BOOLEAN: - boolean valueBool = wikitty.getFieldAsBoolean( - ext.getName(), fieldName); - temp.setType("boolean"); - temp.setValue(valueBool); - - break; - default: - Object valueObject = wikitty.getFieldAsObject( - ext.getName(), fieldName); - String valueString = ""; - - if (valueObject != null) { - valueString = String.valueOf(valueObject); - } - - temp.setValue(valueString); - - valueString = StringEscapeUtils.escapeHtml(valueString); - if (valueString.contains("\n") - || "true" - .equals(fieldType.getTagValue("multiline"))) { - temp.setType("textarea"); - - } else { - temp.setType("textfield"); - - } - - } + // add element to the map log.debug("Add field : " + temp); mapField.put(temp.getName(), temp); } } - /* - * save the field that will be write only if write befeore is selected - * because that mean that this method will be called two times when - * write the openning template of the wikitty field and when it close - * the wikitty field - */ - if (orderBefore) { - log.debug("Order before select, add field to addedfield set: " - + mapField.keySet()); - fieldAdded.addAll(mapField.keySet()); - } - String[] fieldOrder = StringUtil.split(order, ","); + Collection<ExtensionFieldStrutsBean> result = mapField.values(); + /* * if an order is define parse order and construct ordering result if - * all the field are not defined in the order juste add the rest at the + * all the field are not defined in the order just add the rest at the * end of the result list. */ - if (fieldOrder.length != 0) { + if (fieldOrder.length != 0 && !orderBefore) { List<ExtensionFieldStrutsBean> orderedResult = new LinkedList<ExtensionFieldStrutsBean>(); + // fieldname have the classic fq field name format like: + // wikittyextension.name for (String orderIt : fieldOrder) { if (mapField.containsKey(orderIt)) { @@ -225,14 +178,62 @@ } } - + orderedResult.addAll(mapField.values()); - return orderedResult; + + result = orderedResult; } - return mapField.values(); + return result; } + protected Collection<ExtensionFieldStrutsBean> getFieldOrderedBefore() { + + List<ExtensionFieldStrutsBean> result = new LinkedList<ExtensionFieldStrutsBean>(); + + String[] fieldOrder = StringUtil.split(order, ","); + + /* + * this method is used because ordering before is different. + * We parse the list of the element we want ordered.4 + * If we see a * that mean all the field have to be ordered now + * then add those field to the added field + */ + + if (fieldOrder.length != 0) { + for (String fieldit : fieldOrder) { + + if (!isIncluded(fieldit)){ + continue; + } + String extname = WikittyUtil + .getExtensionNameFromFQFieldName(fieldit); + String field = WikittyUtil.getFieldNameFromFQFieldName(fieldit); + // harvest all field of the extension + if ("*".equals(field)) { + + WikittyExtension ext = wikitty.getExtension(extname); + for (String fieldNameIt : ext.getFieldNames()) { + ExtensionFieldStrutsBean temp = constructExtensionFieldFromField( + extname, fieldNameIt); + result.add(temp); + fieldAdded.add(temp.getName()); + } + + } else { + ExtensionFieldStrutsBean temp = constructExtensionFieldFromField( + extname, field); + result.add(temp); + fieldAdded.add(temp.getName()); + } + + } + } + + + return result; + } + /** * construct map for exclude or include template * @@ -272,33 +273,90 @@ fieldAdded.add(field); } - public boolean isIncluded(String fieldname) { + public boolean isIncluded(String fqFieldName) { - String[] fields = StringUtil.split(fieldname, "."); - + String[] fields = StringUtil.split(fqFieldName, "."); + if (fields.length != 2) { // TODO mfortun-2011-06-29 exception } String extName = fields[0]; String fieldName = fields[1]; - + return this.isIncluded(extName, fieldName); + } + + public boolean isIncluded(String extName, String fieldName) { + // check if field is specifically exluded or if all the extention is boolean excluded = excludeMap.containsKey(extName) - && (excludeMap.get(extName).contains(fieldName) || excludeMap - .get(extName).contains("*")); - - // check if field is specifically included or if all the extention is - boolean included = includeEnable - && includeMap.containsKey(extName) - && !(includeMap.get(extName).contains(fieldName) || includeMap - .get(extName).contains("*")); + && (excludeMap.get(extName).contains("*") || excludeMap.get( + extName).contains(fieldName)); + + // check if field is specifically included or if all the extention is + boolean notIncluded = !(includeEnable + && includeMap.containsKey(extName) && (includeMap.get(extName) + .contains("*") || includeMap.get(extName).contains(fieldName))); // check if field allready added - boolean added = fieldAdded.contains(fieldname); - if (excluded || included || added) { + boolean added = fieldAdded.contains(extName + "." + fieldName); + + log.debug("extension: " + extName + " field: " + fieldName + + "notInclude:" + notIncluded + " exclude:" + excluded + + " alreadyAdd:" + added); + + if (excluded || notIncluded || added) { return false; } return true; } + + protected ExtensionFieldStrutsBean constructExtensionFieldFromField( + String extName, String fieldName) { + + ExtensionFieldStrutsBean result = new ExtensionFieldStrutsBean(); + + FieldType fieldType = wikitty.getExtension(extName).getFieldType( + fieldName); + + result.setName(extName + "." + fieldName); + result.setLabel(result.getName()); + result.setValue(""); + + // set the field type. + switch (fieldType.getType()) { + case BINARY: + result.setType("file"); + + break; + case BOOLEAN: + boolean valueBool = wikitty.getFieldAsBoolean(extName, fieldName); + result.setType("boolean"); + result.setValue(valueBool); + + break; + default: + Object valueObject = wikitty.getFieldAsObject(extName, fieldName); + String valueString = ""; + + if (valueObject != null) { + valueString = String.valueOf(valueObject); + } + + result.setValue(valueString); + + valueString = StringEscapeUtils.escapeHtml(valueString); + if (valueString.contains("\n") + || "true".equals(fieldType.getTagValue("multiline"))) { + result.setType("textarea"); + + } else { + result.setType("textfield"); + + } + + } + return result; + } + }
participants (1)
-
mfortun@users.nuiton.org