/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.dynamic.data.mapping.type.checkbox.multiple.internal; import com.liferay.dynamic.data.mapping.form.field.type.DDMFormFieldValueRenderer; import com.liferay.dynamic.data.mapping.model.DDMFormField; import com.liferay.dynamic.data.mapping.model.DDMFormFieldOptions; import com.liferay.dynamic.data.mapping.model.LocalizedValue; import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue; import com.liferay.portal.kernel.json.JSONArray; import com.liferay.portal.kernel.util.StringBundler; import com.liferay.portal.kernel.util.StringPool; import java.util.Locale; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; /** * @author Dylan Rebelak */ @Component( immediate = true, property = {"ddm.form.field.type.name=checkbox_multiple"} ) public class CheckboxMultipleDDMFormFieldValueRenderer implements DDMFormFieldValueRenderer { @Override public String render(DDMFormFieldValue ddmFormFieldValue, Locale locale) { JSONArray optionsValuesJSONArray = checkboxMultipleDDMFormFieldValueAccessor.getValue( ddmFormFieldValue, locale); DDMFormFieldOptions ddmFormFieldOptions = getDDMFormFieldOptions( ddmFormFieldValue); if (optionsValuesJSONArray.length() == 0) { return StringPool.BLANK; } StringBundler sb = new StringBundler( optionsValuesJSONArray.length() * 2 - 1); for (int i = 0; i < optionsValuesJSONArray.length(); i++) { LocalizedValue optionLabel = ddmFormFieldOptions.getOptionLabels( optionsValuesJSONArray.getString(i)); if (optionLabel != null) { sb.append(optionLabel.getString(locale)); sb.append(StringPool.COMMA_AND_SPACE); } } if (sb.index() > 0) { sb.setIndex(sb.index() - 1); } return sb.toString(); } protected DDMFormFieldOptions getDDMFormFieldOptions( DDMFormFieldValue ddmFormFieldValue) { DDMFormField ddmFormField = ddmFormFieldValue.getDDMFormField(); return ddmFormField.getDDMFormFieldOptions(); } @Reference protected CheckboxMultipleDDMFormFieldValueAccessor checkboxMultipleDDMFormFieldValueAccessor; }