protected Field createField( DDMStructure ddmStructure, String fieldName, List<Serializable> fieldValues, ServiceContext serviceContext) { Field field = new Field(); field.setDDMStructureId(ddmStructure.getStructureId()); String languageId = GetterUtil.getString( serviceContext.getAttribute("languageId"), serviceContext.getLanguageId()); Locale locale = LocaleUtil.fromLanguageId(languageId); String defaultLanguageId = GetterUtil.getString(serviceContext.getAttribute("defaultLanguageId")); Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId); if (fieldName.startsWith(StringPool.UNDERLINE)) { locale = LocaleUtil.getSiteDefault(); defaultLocale = LocaleUtil.getSiteDefault(); } field.setDefaultLocale(defaultLocale); field.setName(fieldName); field.setValues(locale, fieldValues); return field; }
@Override public String getXML(Document document, Fields fields) { Element rootElement = null; try { if (document != null) { rootElement = document.getRootElement(); } else { document = _saxReader.createDocument(); rootElement = document.addElement("root"); } Iterator<Field> itr = fields.iterator(true); while (itr.hasNext()) { Field field = itr.next(); List<Node> nodes = getElementsByName(document, field.getName()); for (Node node : nodes) { document.remove(node); } appendField(rootElement, field); } return document.formattedString(); } catch (IOException ioe) { throw new SystemException(ioe); } }
protected Field createFieldsDisplayField(long ddmStructureId, String value) { Field fieldsDisplayField = new Field( ddmStructureId, DDMImpl.FIELDS_DISPLAY_NAME, createValuesList(value), LocaleUtil.US); fieldsDisplayField.setDefaultLocale(LocaleUtil.US); return fieldsDisplayField; }
protected void appendField(Element element, Field field) { Element dynamicElementElement = element.addElement("dynamic-element"); dynamicElementElement.addAttribute( "default-language-id", LocaleUtil.toLanguageId(field.getDefaultLocale())); dynamicElementElement.addAttribute("name", field.getName()); for (Locale locale : field.getAvailableLocales()) { List<Serializable> values = field.getValues(locale); for (Serializable value : values) { Element dynamicContentElement = dynamicElementElement.addElement("dynamic-content"); dynamicContentElement.addAttribute("language-id", LocaleUtil.toLanguageId(locale)); updateField(dynamicContentElement, value); } } }
protected Map<Locale, List<Serializable>> getMergedFieldValuesMap( Field newField, String[] newFieldsDisplayValues, Field existingField, String[] existingFieldsDisplayValues) { Set<Locale> availableLocales = getMergedAvailableLocales( newField.getAvailableLocales(), existingField.getAvailableLocales()); for (Locale locale : availableLocales) { List<Serializable> newFieldValues = getFieldValues(newField, locale); List<Serializable> existingFieldValues = getFieldValues(existingField, locale); List<Serializable> defaultFieldValues = getFieldValues(newField, newField.getDefaultLocale()); List<Serializable> mergedLocaleValues = getMergedFieldValues( newField.getName(), newFieldValues, newFieldsDisplayValues, existingFieldValues, existingFieldsDisplayValues, defaultFieldValues); existingField.setValues(locale, mergedLocaleValues); } return existingField.getValuesMap(); }
@Override public String[] getFieldsDisplayValues(Field fieldsDisplayField) throws Exception { DDMStructure ddmStructure = fieldsDisplayField.getDDMStructure(); List<String> fieldsDisplayValues = new ArrayList<>(); String[] values = splitFieldsDisplayValue(fieldsDisplayField); for (String value : values) { String fieldName = StringUtil.extractFirst(value, DDMImpl.INSTANCE_SEPARATOR); if (ddmStructure.hasField(fieldName)) { fieldsDisplayValues.add(fieldName); } } return fieldsDisplayValues.toArray(new String[fieldsDisplayValues.size()]); }
@Override public Fields mergeFields(Fields newFields, Fields existingFields) { String[] newFieldsDisplayValues = splitFieldsDisplayValue(newFields.get(DDMImpl.FIELDS_DISPLAY_NAME)); String[] existingFieldsDisplayValues = splitFieldsDisplayValue(existingFields.get(DDMImpl.FIELDS_DISPLAY_NAME)); Iterator<Field> itr = newFields.iterator(true); while (itr.hasNext()) { Field newField = itr.next(); Field existingField = existingFields.get(newField.getName()); if (existingField == null) { existingFields.put(newField); continue; } if (newField.isPrivate()) { continue; } existingField.setDefaultLocale(newField.getDefaultLocale()); Map<Locale, List<Serializable>> mergedFieldValuesMap = getMergedFieldValuesMap( newField, newFieldsDisplayValues, existingField, existingFieldsDisplayValues); existingField.setValuesMap(mergedFieldValuesMap); } return existingFields; }
@Override public Fields getFields(DDMStructure structure, XPath xPath, String xml, List<String> fieldNames) throws PortalException { Document document = null; try { document = _saxReader.read(xml); } catch (DocumentException de) { if (_log.isDebugEnabled()) { _log.debug(de.getMessage(), de); } return null; } if ((xPath != null) && !xPath.booleanValueOf(document)) { return null; } Fields fields = new Fields(); Element rootElement = document.getRootElement(); List<Element> dynamicElementElements = rootElement.elements("dynamic-element"); for (Element dynamicElementElement : dynamicElementElements) { String fieldName = dynamicElementElement.attributeValue("name"); if (!structure.hasField(fieldName) || ((fieldNames != null) && !fieldNames.contains(fieldName))) { continue; } String fieldDataType = structure.getFieldDataType(fieldName); List<Element> dynamicContentElements = dynamicElementElement.elements("dynamic-content"); for (Element dynamicContentElement : dynamicContentElements) { String fieldValue = dynamicContentElement.getText(); String languageId = dynamicContentElement.attributeValue("language-id"); Locale locale = LocaleUtil.fromLanguageId(languageId); Serializable fieldValueSerializable = FieldConstants.getSerializable(fieldDataType, fieldValue); Field field = fields.get(fieldName); if (field == null) { field = new Field(); String defaultLanguageId = dynamicElementElement.attributeValue("default-language-id"); if (Validator.isNull(defaultLanguageId)) { defaultLanguageId = rootElement.attributeValue("default-locale"); } Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId); field.setDefaultLocale(defaultLocale); field.setDDMStructureId(structure.getStructureId()); field.setName(fieldName); field.setValue(locale, fieldValueSerializable); fields.put(field); } else { field.addValue(locale, fieldValueSerializable); } } } return fields; }
protected List<Serializable> getFieldValues(Field field, Locale locale) { Map<Locale, List<Serializable>> fieldValuesMap = field.getValuesMap(); return fieldValuesMap.get(locale); }
protected String[] splitFieldsDisplayValue(Field fieldsDisplayField) { String value = (String) fieldsDisplayField.getValue(); return StringUtil.split(value); }