| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IdTranslatorAssemblerFilter | 
 | 
 | 6.0;6 | 
| 1 |  /** | |
| 2 |   * Copyright 2010 The Kuali Foundation Licensed under the | |
| 3 |   * Educational Community License, Version 2.0 (the "License"); you may | |
| 4 |   * not use this file except in compliance with the License. You may | |
| 5 |   * obtain a copy of the License at | |
| 6 |   * | |
| 7 |   * http://www.osedu.org/licenses/ECL-2.0 | |
| 8 |   * | |
| 9 |   * Unless required by applicable law or agreed to in writing, | |
| 10 |   * software distributed under the License is distributed on an "AS IS" | |
| 11 |   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | |
| 12 |   * or implied. See the License for the specific language governing | |
| 13 |   * permissions and limitations under the License. | |
| 14 |   */ | |
| 15 | ||
| 16 |  package org.kuali.student.common.assembly.old; | |
| 17 | ||
| 18 |  import java.util.Iterator; | |
| 19 | ||
| 20 |  import org.apache.commons.lang.StringUtils; | |
| 21 |  import org.apache.log4j.Logger; | |
| 22 |  import org.kuali.student.common.assembly.data.AssemblyException; | |
| 23 |  import org.kuali.student.common.assembly.data.Data; | |
| 24 |  import org.kuali.student.common.assembly.data.Metadata; | |
| 25 |  import org.kuali.student.common.assembly.data.Data.DataType; | |
| 26 |  import org.kuali.student.common.assembly.data.Data.Property; | |
| 27 |  import org.kuali.student.common.assembly.old.data.SaveResult; | |
| 28 |  import org.kuali.student.common.assembly.util.IdTranslation; | |
| 29 |  import org.kuali.student.common.assembly.util.IdTranslator; | |
| 30 | ||
| 31 |  /** | |
| 32 |   * Intercepts the result of the target Assembler's get(...) method and translates IDs | |
| 33 |   *  | |
| 34 |   * @author Kuali Student Team | |
| 35 |   */ | |
| 36 | @Deprecated | |
| 37 | public class IdTranslatorAssemblerFilter extends PassThroughAssemblerFilter<Data, Void> { | |
| 38 | ||
| 39 | 0 |      final Logger LOG = Logger.getLogger(IdTranslatorAssemblerFilter.class); | 
| 40 | ||
| 41 | ||
| 42 |          private IdTranslator idTranslator; | |
| 43 | ||
| 44 | 0 |      public IdTranslatorAssemblerFilter(IdTranslator idTranslator) { | 
| 45 | 0 |          this.idTranslator = idTranslator; | 
| 46 | 0 |      } | 
| 47 | ||
| 48 | @Override | |
| 49 | public void doGetFilter(FilterParamWrapper<String> id, FilterParamWrapper<Data> response, GetFilterChain<Data, Void> chain) throws AssemblyException { | |
| 50 | 0 |          chain.doGetFilter(id, response); | 
| 51 | 0 |          Data data = response.getValue();  | 
| 52 | 0 |          if (data != null) { | 
| 53 | 0 |              translateIds(data, chain); | 
| 54 | } | |
| 55 | 0 |      } | 
| 56 | ||
| 57 | @Override | |
| 58 | public void doSaveFilter(FilterParamWrapper<Data> request, FilterParamWrapper<SaveResult<Data>> response, SaveFilterChain<Data, Void> chain) throws AssemblyException { | |
| 59 | 0 |          chain.doSaveFilter(request, response); | 
| 60 | 0 |          SaveResult<Data> saveResult = response.getValue(); | 
| 61 | 0 |          Data data = saveResult != null && saveResult.getValue() != null ? saveResult.getValue() : null; | 
| 62 | 0 |          if(data != null) { | 
| 63 | 0 |              translateIds(data, chain); | 
| 64 | } | |
| 65 | 0 |      } | 
| 66 | ||
| 67 | private void translateIds(Data data, AssemblerManagerAccessable<Data, Void> chain) throws AssemblyException { | |
| 68 | 0 |          Assembler<Data, Void> a = chain.getManager().getTarget(); | 
| 69 | 0 |          Metadata metadata = a.getDefaultMetadata(); | 
| 70 | 0 |          if (metadata != null && data != null) { | 
| 71 | 0 |              __translateIds(data, metadata); | 
| 72 | } | |
| 73 | 0 |      } | 
| 74 | ||
| 75 |      /** | |
| 76 |       * Uses the IdTranslator and Metadata to convert IDs into their display text and stores those translations in the | |
| 77 |       * _runtimeData node | |
| 78 |       *  | |
| 79 |       * @param data | |
| 80 |       *            the Data instance containing IDs to be translated | |
| 81 |       * @param metadata | |
| 82 |       *            the Metadata instance representing the data provided. | |
| 83 |       * @throws AssemblyException | |
| 84 |       */ | |
| 85 | private void __translateIds(Data data, Metadata metadata) | |
| 86 |                          throws AssemblyException { | |
| 87 |                  try{ | |
| 88 | 0 |                          if (data != null && metadata != null) { | 
| 89 |                                  //Iterate through all the data;s properties | |
| 90 | 0 |                                  for (Iterator<Property> iter = data.realPropertyIterator(); iter.hasNext();) { | 
| 91 | 0 |                                          Property prop = iter.next(); | 
| 92 | ||
| 93 | 0 |                                          Object fieldData = prop.getValue(); | 
| 94 | 0 |                                          Object fieldKey = prop.getKey(); | 
| 95 | ||
| 96 | 0 |                                          Metadata fieldMetadata = metadata.getProperties().get(fieldKey); | 
| 97 | ||
| 98 |                                          //if the fieldMetadata is null then try to use the parent metadata as in the case of lists | |
| 99 | 0 |                                          if(fieldMetadata==null){ | 
| 100 | 0 |                                                  fieldMetadata=metadata; | 
| 101 | } | |
| 102 | ||
| 103 |                                          //If the fieldData is Data itself the recurse | |
| 104 | 0 |                                          if (fieldData instanceof Data) { | 
| 105 | 0 |                                                  if (DataType.LIST.equals(fieldMetadata.getDataType())) { | 
| 106 |                                                          //Lists are a special case where the metadata property name is "*" | |
| 107 | 0 |                                                          Metadata listChildMetadata = fieldMetadata.getProperties().get("*"); | 
| 108 |                                                          //see if this is a list of data or a list of fields | |
| 109 | 0 |                                                          if(DataType.DATA.equals(listChildMetadata.getDataType())){ | 
| 110 | 0 |                                                                  __translateIds((Data) prop.getValue(), listChildMetadata); | 
| 111 |                                                          }else{ | |
| 112 |                                                                  //its a list of fields so loop through and translate using the "index" | |
| 113 | 0 |                                                                  for(Iterator<Property> listIter = ((Data)fieldData).realPropertyIterator(); listIter.hasNext();){ | 
| 114 | 0 |                                                                          Property listItem = listIter.next(); | 
| 115 | 0 |                                                                          Object listData = listItem.getValue(); | 
| 116 | 0 |                                                                          if (listData != null && listData instanceof String) { | 
| 117 | 0 |                                                                                  if (fieldMetadata.getInitialLookup() != null | 
| 118 |                                                                                                  && !StringUtils.isEmpty((String) listData)) { | |
| 119 |                                                                                          //This is a string with a lookup so do the translation | |
| 120 | 0 |                                                                                          IdTranslation trans = idTranslator.getTranslation(fieldMetadata.getInitialLookup(),        (String) listData); | 
| 121 | 0 |                                                                                          if (trans != null) { | 
| 122 | 0 |                                                                                                  Integer index = listItem.getKey(); | 
| 123 | 0 |                                                                                                  setTranslation((Data)fieldData, listItem.getKey().toString(), index, trans.getDisplay()); | 
| 124 | } | |
| 125 | } | |
| 126 | } | |
| 127 | 0 |                                                                  } | 
| 128 | } | |
| 129 | 0 |                                                  } else { | 
| 130 |                                                          //Otherwise just use the fieldMetadata | |
| 131 | 0 |                                                          __translateIds((Data) prop.getValue(), fieldMetadata); | 
| 132 | } | |
| 133 | 0 |                                          } else if (fieldData != null && fieldData instanceof String) { | 
| 134 | 0 |                                                  if (fieldMetadata.getInitialLookup() != null | 
| 135 |                                                                  && !StringUtils.isEmpty((String) fieldData)) { | |
| 136 |                                                          //This is a string with a lookup so do the translation | |
| 137 | 0 |                                                          IdTranslation trans = idTranslator.getTranslation(fieldMetadata.getInitialLookup(),        (String) fieldData); | 
| 138 | 0 |                                                          if (trans != null) { | 
| 139 | 0 |                                                                  setTranslation(data, prop.getKey().toString(), null, trans.getDisplay()); | 
| 140 | } | |
| 141 | } | |
| 142 | } | |
| 143 | 0 |                                  } | 
| 144 | } | |
| 145 | 0 |                  }catch(Exception e){ | 
| 146 | 0 |                          LOG.error("Error translating", e); | 
| 147 | 0 |                  } | 
| 148 | 0 |          } | 
| 149 | ||
| 150 | private static void setTranslation(Data data, String field, Integer index, String translation) { | |
| 151 | 0 |          if (data != null) { | 
| 152 |                  //Get runtime data for the node and create if it doesn't exist | |
| 153 | 0 |              Data runtime = data.get("_runtimeData"); | 
| 154 | 0 |              if (runtime == null) { | 
| 155 | 0 |                  runtime = new Data(); | 
| 156 | 0 |                  data.set("_runtimeData", runtime); | 
| 157 | } | |
| 158 | 0 |              if(index != null) { | 
| 159 |                  //If the index is set this is a list item (foo/bar/0/, foo/bar/1/) | |
| 160 | 0 |                  Data fieldIndexData = runtime.get(index); | 
| 161 | 0 |                  if(fieldIndexData==null){ | 
| 162 | 0 |                          fieldIndexData = new Data(); | 
| 163 | 0 |                      runtime.set(index, fieldIndexData); | 
| 164 | } | |
| 165 | 0 |                  fieldIndexData.set("id-translation", translation); | 
| 166 | 0 |              }else{ | 
| 167 |                      //Otherwise set the translation directly referenced by the field | |
| 168 |                  //If the index is set this is a list item (foo/bar/0/, foo/bar/1/) | |
| 169 | 0 |                  Data fieldData = runtime.get(field); | 
| 170 | 0 |                  if(fieldData==null){ | 
| 171 | 0 |                          fieldData = new Data();         | 
| 172 | 0 |                      runtime.set(field, fieldData); | 
| 173 | } | |
| 174 | 0 |                  fieldData.set("id-translation", translation); | 
| 175 | } | |
| 176 | } | |
| 177 | 0 |      } | 
| 178 | } |