| 1 | |
package org.kuali.student.lum.common.client.lo; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.HashMap; |
| 5 | |
import java.util.Iterator; |
| 6 | |
import java.util.List; |
| 7 | |
import java.util.Map; |
| 8 | |
|
| 9 | |
import org.kuali.student.common.assembly.data.Data; |
| 10 | |
import org.kuali.student.common.assembly.data.QueryPath; |
| 11 | |
import org.kuali.student.common.ui.client.configurable.mvc.binding.ModelWidgetBindingSupport; |
| 12 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
| 13 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | 0 | public class LOBuilderBinding extends ModelWidgetBindingSupport<LOBuilder> { |
| 19 | |
|
| 20 | 0 | public static LOBuilderBinding INSTANCE = new LOBuilderBinding(); |
| 21 | |
|
| 22 | 0 | private LOBuilderBinding() {} |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
@Override |
| 30 | |
public void setModelValue(LOBuilder builder, DataModel model, String path) { |
| 31 | 0 | Data losData = new Data(); |
| 32 | 0 | Map<Integer, Data> parentStore = new HashMap<Integer, Data>(); |
| 33 | 0 | int sequence = 0; |
| 34 | 0 | List<OutlineNode<LOPicker>> value = stripOutEmptyInput(builder.getValue()); |
| 35 | 0 | if (value != null) { |
| 36 | 0 | for (OutlineNode<LOPicker> node : value) { |
| 37 | 0 | if (node.getIndentLevel() == 0) { |
| 38 | 0 | Data item = createLoDisplayInfoData(node, sequence); |
| 39 | 0 | parentStore.put(new Integer(0), item); |
| 40 | 0 | losData.add(item); |
| 41 | 0 | } else { |
| 42 | 0 | Data item = createLoDisplayInfoData(node, sequence); |
| 43 | 0 | LoDisplayInfoHelper parentItemHelper = null; |
| 44 | 0 | parentStore.put(node.getIndentLevel(), item); |
| 45 | 0 | parentItemHelper = new LoDisplayInfoHelper( |
| 46 | |
parentStore.get(node.getIndentLevel() - 1)); |
| 47 | 0 | parentItemHelper.getDisplayInfoList().add(item); |
| 48 | |
} |
| 49 | 0 | sequence++; |
| 50 | |
} |
| 51 | |
} |
| 52 | 0 | model.set(QueryPath.parse(path), losData); |
| 53 | 0 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
public void setWidgetValue(LOBuilder builder, DataModel model, String path) { |
| 57 | 0 | List<OutlineNode<LOPicker>> loOutlineNodes = new ArrayList<OutlineNode<LOPicker>>(); |
| 58 | |
|
| 59 | |
|
| 60 | 0 | QueryPath qPath = QueryPath.parse(path); |
| 61 | |
|
| 62 | 0 | Data data = null; |
| 63 | 0 | if (model != null) { |
| 64 | 0 | data = model.get(qPath); |
| 65 | |
} |
| 66 | |
|
| 67 | 0 | dataToOutlineNodes(data, loOutlineNodes, 0); |
| 68 | 0 | builder.setValue(loOutlineNodes); |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
private List<OutlineNode<LOPicker>> stripOutEmptyInput(List<OutlineNode<LOPicker>> input) { |
| 72 | 0 | List<OutlineNode<LOPicker>> value = new ArrayList<OutlineNode<LOPicker>>(); |
| 73 | 0 | boolean allEmptyNodes = true; |
| 74 | 0 | if (input != null) { |
| 75 | 0 | for (OutlineNode<LOPicker> node : input) { |
| 76 | 0 | String desc = node.getUserObject().getLOText(); |
| 77 | 0 | int indentLevel = node.getIndentLevel(); |
| 78 | 0 | List<LoCategoryInfo> categories = node.getUserObject().getLoCategories(); |
| 79 | 0 | if (desc != null && desc.trim().length() > 0 || indentLevel > 0 || categories != null && !categories.isEmpty()) { |
| 80 | 0 | allEmptyNodes = false; |
| 81 | 0 | value.add(node); |
| 82 | |
} |
| 83 | 0 | } |
| 84 | |
} |
| 85 | 0 | if (allEmptyNodes) { |
| 86 | 0 | value = null; |
| 87 | |
} |
| 88 | 0 | return value; |
| 89 | |
} |
| 90 | |
|
| 91 | |
private void dataToOutlineNodes(Data data, List<OutlineNode<LOPicker>> loOutlineNodes, int identLevel) { |
| 92 | 0 | if (data != null) { |
| 93 | 0 | LoDisplayInfoSortedSet sortedDisplayInfos = new LoDisplayInfoSortedSet(); |
| 94 | 0 | for (Data.Property property : data) { |
| 95 | 0 | Data loDisplayInfoData = property.getValue(); |
| 96 | 0 | LoDisplayInfoHelper loDisplayInfoHelper = new LoDisplayInfoHelper(loDisplayInfoData); |
| 97 | 0 | sortedDisplayInfos.add(loDisplayInfoHelper); |
| 98 | 0 | } |
| 99 | 0 | for (LoDisplayInfoHelper loDisplayInfoHelper : sortedDisplayInfos) { |
| 100 | 0 | LOPicker picker = new LOPicker(LOBuilder.getMessageGroup(), LOBuilder.getType(), LOBuilder.getState(), LOBuilder.getRepoKey()); |
| 101 | |
|
| 102 | 0 | LoInfoHelper loInfoHelper = new LoInfoHelper(loDisplayInfoHelper.getLoInfo()); |
| 103 | 0 | RichTextHelper descriptionHelper = new RichTextHelper(loInfoHelper.getDesc()); |
| 104 | 0 | picker.setLOText(descriptionHelper.getPlain()); |
| 105 | 0 | List<LoCategoryInfo> categories = getCategoryList(loDisplayInfoHelper); |
| 106 | 0 | picker.setLOCategories(categories); |
| 107 | 0 | picker.setMetaInfoData(loInfoHelper.getMetaInfo()); |
| 108 | 0 | OutlineNode<LOPicker> node = new OutlineNode<LOPicker>(); |
| 109 | |
|
| 110 | 0 | node.setUserObject(picker); |
| 111 | 0 | node.setOpaque(loInfoHelper.getId()); |
| 112 | 0 | node.setIndentLevel(identLevel); |
| 113 | 0 | loOutlineNodes.add(node); |
| 114 | |
|
| 115 | 0 | dataToOutlineNodes(loDisplayInfoHelper.getDisplayInfoList(), loOutlineNodes, identLevel + 1); |
| 116 | 0 | } |
| 117 | |
} |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
private List<LoCategoryInfo> getCategoryList(LoDisplayInfoHelper loDisplayInfoHelper) { |
| 121 | 0 | List<LoCategoryInfo> categoryInfos = new ArrayList<LoCategoryInfo>(); |
| 122 | 0 | Data categoriesData = loDisplayInfoHelper.getCategoryInfoList(); |
| 123 | |
|
| 124 | 0 | if (null != categoriesData) { |
| 125 | 0 | Iterator<Data.Property> itr = categoriesData.realPropertyIterator(); |
| 126 | |
|
| 127 | 0 | while (itr.hasNext()) { |
| 128 | 0 | Data.Property catProp = itr.next(); |
| 129 | 0 | Data catData = catProp.getValue(); |
| 130 | 0 | LoCategoryInfo catInfo = CategoryDataUtil.toLoCategoryInfo(catData); |
| 131 | 0 | categoryInfos.add(catInfo); |
| 132 | 0 | } |
| 133 | |
} |
| 134 | 0 | return categoryInfos; |
| 135 | |
} |
| 136 | |
|
| 137 | |
private Data createLoDisplayInfoData(OutlineNode<LOPicker> node, int sequence) { |
| 138 | 0 | Data result = null; |
| 139 | 0 | LoDisplayInfoHelper loDisplayInfoDataHelper = new LoDisplayInfoHelper(new Data()); |
| 140 | 0 | LoInfoHelper loInfoHelper = new LoInfoHelper(new Data()); |
| 141 | |
|
| 142 | 0 | loInfoHelper.setId((String) node.getOpaque()); |
| 143 | |
|
| 144 | 0 | RichTextHelper richTextHelper = new RichTextHelper(); |
| 145 | 0 | String loDesc = node.getUserObject().getLOText(); |
| 146 | 0 | richTextHelper.setFormatted(loDesc); |
| 147 | 0 | richTextHelper.setPlain(loDesc); |
| 148 | 0 | loInfoHelper.setDesc(richTextHelper.getData()); |
| 149 | |
|
| 150 | 0 | if (null == loInfoHelper.getName() || loInfoHelper.getName().length() == 0) { |
| 151 | 0 | loInfoHelper.setName("SINGLE USE LO"); |
| 152 | |
} |
| 153 | |
|
| 154 | |
|
| 155 | 0 | Data categoriesData = new Data(); |
| 156 | 0 | for (LoCategoryInfo cat : node.getUserObject().getLoCategories()) { |
| 157 | 0 | categoriesData.add(CategoryDataUtil.toData(cat)); |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | 0 | loInfoHelper.setSequence(Integer.toString(sequence)); |
| 162 | |
|
| 163 | |
|
| 164 | 0 | loInfoHelper.setMetaInfo(node.getUserObject().getMetaInfoData()); |
| 165 | |
|
| 166 | 0 | loDisplayInfoDataHelper.setLoInfo(loInfoHelper.getData()); |
| 167 | 0 | loDisplayInfoDataHelper.setCategoryInfoList(categoriesData); |
| 168 | 0 | result = loDisplayInfoDataHelper.getData(); |
| 169 | 0 | return result; |
| 170 | |
} |
| 171 | |
|
| 172 | |
} |