List of usage examples for com.liferay.portal.kernel.json JSONObject has
public boolean has(String key);
From source file:com.beorn.paymentpluginapi.config.ConfigDescription.java
License:Open Source License
/** * Verifies if a configuration is valid for the description * /* www.j av a 2s . c o m*/ * @param configObject * the configuration to check against the description * @return a boolean indicating if the configuration is valid or not for the * description */ public boolean isValid(JSONObject configObject) { for (ConfigGroup configGroup : _configGroups) { if (!configObject.has(configGroup.getKey())) return false; JSONObject configObjectGroup = configObject.getJSONObject(configGroup.getKey()); for (ConfigParameter configParameter : configGroup.getConfigParameters()) { if (!configObjectGroup.has(configParameter.getKey())) return false; // Extra validation can be added here (required parameters, // etc...) } } return true; }
From source file:com.beorn.paymentpluginapi.test.ConfigValidatorTestBase.java
License:Open Source License
@Test public void testNullConfig() throws Exception { String configString = "configString"; mockStatic(JSONFactoryUtil.class); JSONObject config = mock(JSONObject.class); when(JSONFactoryUtil.createJSONObject(configString)).thenReturn(config); when(config.has(Mockito.anyString())).thenReturn(true); when(config.getJSONObject(Mockito.anyString())).thenReturn(config); ConfigValidator validator = getValidator(); Assert.assertTrue(validator.isValid(configString)); }
From source file:com.beorn.paypalpaymentplugin.util.PayPalConfigUtil.java
License:Open Source License
private static void putPropertiesFromGroup(Map<String, String> configurationMap, JSONObject config, String groupName, String[] propertiesName) throws InvalidConfigException { if (!config.has(groupName)) throw new InvalidConfigException("Missing group \"" + groupName + "\""); JSONObject groupConfig = config.getJSONObject(groupName); for (String propertyName : propertiesName) putProperty(configurationMap, groupConfig, propertyName); }
From source file:com.beorn.paypalpaymentplugin.util.PayPalConfigUtil.java
License:Open Source License
private static void putProperty(Map<String, String> configurationMap, JSONObject config, String name) throws InvalidConfigException { if (!config.has(name)) throw new InvalidConfigException("Missing parameter \"" + name + "\""); String value = config.getString(name); if (Validator.isNotNull(value)) configurationMap.put(name, value); }
From source file:com.evozon.evoportal.my_account.wrapper.UserExpandoWrapper.java
public int getRemainingFreeDaysFromYear(int year) { int remainingFromYear = 0; try {/*w w w. j ava 2 s .com*/ String remDaysFromLastStr = user.getExpandoBridge() .getAttribute(MyAccountConstants.REMAINING_FREE_DAYS_LAST, false).toString(); if (!remDaysFromLastStr.isEmpty()) { JSONObject yearsSituation = JSONFactoryUtil.createJSONObject(remDaysFromLastStr); String pastYearStr = String.valueOf(year); if (yearsSituation.has(pastYearStr)) { JSONObject yearJSON = yearsSituation.getJSONObject(pastYearStr); remainingFromYear = yearJSON.getInt(MyAccountConstants.DAYS_LEFT); } } } catch (JSONException e) { log.error("Couldn't retrieve remaining free days from last year for user: " + user.getFullName()); } return remainingFromYear; }
From source file:com.liferay.adaptive.media.image.internal.util.AdaptiveMediaImageSerializerImpl.java
License:Open Source License
@Override public AdaptiveMedia<AdaptiveMediaImageProcessor> deserialize(String s, Supplier<InputStream> inputStreamSupplier) { try {/*from ww w . j a v a 2 s .co m*/ JSONObject jsonObject = JSONFactoryUtil.createJSONObject(s); Map<String, String> properties = new HashMap<>(); JSONObject attributesJSONObject = jsonObject.getJSONObject("attributes"); Map<String, AdaptiveMediaAttribute<?, ?>> allowedAttributes = AdaptiveMediaImageAttribute .allowedAttributes(); allowedAttributes.forEach((name, attribute) -> { if (attributesJSONObject.has(name)) { properties.put(name, attributesJSONObject.getString(name)); } }); String uri = jsonObject.getString("uri"); return new AdaptiveMediaImage(inputStreamSupplier, AdaptiveMediaImageAttributeMapping.fromProperties(properties), URI.create(uri)); } catch (JSONException jsone) { throw new AdaptiveMediaRuntimeException(jsone); } }
From source file:com.liferay.adaptive.media.image.internal.util.AMImageSerializerImpl.java
License:Open Source License
@Override public AdaptiveMedia<AMImageProcessor> deserialize(String s, Supplier<InputStream> inputStreamSupplier) { try {/*from w ww . ja va 2s . c o m*/ JSONObject jsonObject = JSONFactoryUtil.createJSONObject(s); Map<String, String> properties = new HashMap<>(); JSONObject attributesJSONObject = jsonObject.getJSONObject("attributes"); Map<String, AMAttribute<?, ?>> allowedAMAttributes = AMImageAttribute.getAllowedAMAttributes(); allowedAMAttributes.forEach((name, amAttribute) -> { if (attributesJSONObject.has(name)) { properties.put(name, attributesJSONObject.getString(name)); } }); String uri = jsonObject.getString("uri"); return new AMImage(inputStreamSupplier, AMImageAttributeMapping.fromProperties(properties), URI.create(uri)); } catch (JSONException jsone) { throw new AMRuntimeException(jsone); } }
From source file:com.liferay.dynamic.data.mapping.form.web.internal.display.context.DDMFormAdminDisplayContext.java
License:Open Source License
protected String getJSONObjectLocalizedPropertyFromRequest(String propertyName) { String propertyValue = ParamUtil.getString(formAdminRequestHelper.getRequest(), propertyName); if (Validator.isNull(propertyValue)) { return StringPool.BLANK; }// www .j a v a 2 s.com ThemeDisplay themeDisplay = formAdminRequestHelper.getThemeDisplay(); try { JSONObject jsonObject = _jsonFactory.createJSONObject(propertyValue); String languageId = themeDisplay.getLanguageId(); if (jsonObject.has(languageId)) { return jsonObject.getString(languageId); } return jsonObject.getString(getDefaultLanguageId()); } catch (JSONException jsone) { _log.error(String.format("Unable to deserialize JSON localized property \"%s\" " + "from request", propertyName), jsone); } return StringPool.BLANK; }
From source file:com.liferay.dynamic.data.mapping.internal.util.DDMTemplateHelperImpl.java
License:Open Source License
@Override public String getAutocompleteJSON(HttpServletRequest request, String language) throws Exception { JSONObject jsonObject = _jsonFactory.createJSONObject(); JSONObject typesJSONObject = _jsonFactory.createJSONObject(); JSONObject variablesJSONObject = _jsonFactory.createJSONObject(); for (TemplateVariableDefinition templateVariableDefinition : getAutocompleteTemplateVariableDefinitions( request, language)) {/* w w w.jav a 2 s .c o m*/ Class<?> clazz = templateVariableDefinition.getClazz(); if (clazz == null) { variablesJSONObject.put(templateVariableDefinition.getName(), StringPool.BLANK); } else { if (!typesJSONObject.has(clazz.getName())) { typesJSONObject.put(clazz.getName(), getAutocompleteClassJSONObject(clazz)); } variablesJSONObject.put(templateVariableDefinition.getName(), getAutocompleteVariableJSONObject(clazz)); } } jsonObject.put("types", typesJSONObject); jsonObject.put("variables", variablesJSONObject); return jsonObject.toString(); }
From source file:com.liferay.dynamic.data.mapping.io.internal.DDMFormJSONDeserializerImpl.java
License:Open Source License
protected void setDDMFormFieldProperty(JSONObject jsonObject, DDMFormField ddmFormField, DDMFormField ddmFormFieldTypeSetting) throws PortalException { String settingName = ddmFormFieldTypeSetting.getName(); if (jsonObject.has(settingName)) { Object deserializedDDMFormFieldProperty = deserializeDDMFormFieldProperty( jsonObject.getString(settingName), ddmFormFieldTypeSetting); ddmFormField.setProperty(settingName, deserializedDDMFormFieldProperty); }// ww w .ja va 2 s.c o m }