Search in sources :

Example 1 with JSONException

use of org.opencms.json.JSONException in project opencms-core by alkacon.

the class CmsFileExplorerSettings method getSettingsString.

/**
 * @see org.opencms.ui.apps.I_CmsAppSettings#getSettingsString()
 */
public String getSettingsString() {
    JSONObject json = new JSONObject();
    try {
        json.put(SORT_ORDER_KEY, m_sortAscending);
        json.put(SORT_COLUMN_KEY, m_sortColumnId.getId());
        List<String> collapsed = Lists.newArrayList();
        for (CmsResourceTableProperty column : m_collapsedColumns) {
            collapsed.add(column.getId());
        }
        json.put(COLLAPSED_COLUMNS_KEY, new JSONArray(collapsed));
    } catch (JSONException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
    return json.toString();
}
Also used : CmsResourceTableProperty(org.opencms.ui.components.CmsResourceTableProperty) JSONObject(org.opencms.json.JSONObject) JSONArray(org.opencms.json.JSONArray) JSONException(org.opencms.json.JSONException)

Example 2 with JSONException

use of org.opencms.json.JSONException in project opencms-core by alkacon.

the class CmsWorkplaceAppManager method getUserQuickLauchConfigurations.

/**
 * Returns the quick launch apps set for the current user.<p>
 *
 * @param cms the cms context
 *
 * @return the quick launch app configurations
 */
protected List<I_CmsWorkplaceAppConfiguration> getUserQuickLauchConfigurations(CmsObject cms) {
    String apps_info = (String) cms.getRequestContext().getCurrentUser().getAdditionalInfo(QUICK_LAUCH_APPS_KEY);
    String[] appIds = null;
    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(apps_info)) {
        try {
            JSONArray ids = new JSONArray(apps_info);
            appIds = new String[ids.length()];
            for (int i = 0; i < appIds.length; i++) {
                appIds[i] = ids.getString(i);
            }
        } catch (JSONException e) {
            LOG.error("Error parsing user quick launch apps setting.", e);
            appIds = null;
        }
    }
    return getAppConfigurations(appIds != null ? appIds : DEFAULT_USER_APPS);
}
Also used : JSONArray(org.opencms.json.JSONArray) JSONException(org.opencms.json.JSONException)

Example 3 with JSONException

use of org.opencms.json.JSONException in project opencms-core by alkacon.

the class CmsXmlContentEditor method buildElementChoices.

/**
 * Returns the JSON array with information about the choices of a given element.<p>
 *
 * The returned array is only filled if the given element has choice options, otherwise an empty array is returned.<br/>
 * Note: the first array element is an object containing information if the element itself is a choice type,
 * the following elements are the choice option items.<p>
 *
 * @param elementName the element name to check (complete xpath)
 * @param choiceType flag indicating if the given element name represents a choice type or not
 * @param checkChoice flag indicating if the element name should be checked if it is a choice option and choice type
 *
 * @return the JSON array with information about the choices of a given element
 */
public JSONArray buildElementChoices(String elementName, boolean choiceType, boolean checkChoice) {
    JSONArray choiceElements = new JSONArray();
    String choiceName = elementName;
    I_CmsXmlSchemaType elemType = m_content.getContentDefinition().getSchemaType(elementName);
    if (checkChoice && elemType.isChoiceOption() && elemType.isChoiceType()) {
        // the element itself is a choice option and again a choice type, remove the last element to get correct choices
        choiceName = CmsXmlUtils.removeLastXpathElement(elementName);
    }
    // use xpath to get choice information
    if (m_content.hasChoiceOptions(choiceName, getElementLocale())) {
        // we have choice options, first add information about type to create
        JSONObject info = new JSONObject();
        try {
            // put information if element is a choice type
            info.put("choicetype", choiceType);
            choiceElements.put(info);
            // get the available choice options for the choice element
            List<I_CmsXmlSchemaType> options = m_content.getChoiceOptions(choiceName, getElementLocale());
            for (Iterator<I_CmsXmlSchemaType> i = options.iterator(); i.hasNext(); ) {
                // add the available element options
                I_CmsXmlSchemaType type = i.next();
                JSONObject option = new JSONObject();
                String key = A_CmsWidget.LABEL_PREFIX + type.getContentDefinition().getInnerName() + "." + type.getName();
                // add element name, label and help info
                option.put("name", type.getName());
                option.put("label", keyDefault(key, type.getName()));
                option.put("help", keyDefault(key + A_CmsWidget.HELP_POSTFIX, ""));
                // add info if the choice itself is a (sub) choice type
                option.put("subchoice", type.isChoiceType());
                choiceElements.put(option);
            }
        } catch (JSONException e) {
        // ignore, should not happen
        }
    }
    return choiceElements;
}
Also used : I_CmsXmlSchemaType(org.opencms.xml.types.I_CmsXmlSchemaType) JSONObject(org.opencms.json.JSONObject) JSONArray(org.opencms.json.JSONArray) JSONException(org.opencms.json.JSONException)

Example 4 with JSONException

use of org.opencms.json.JSONException in project opencms-core by alkacon.

the class CmsSerialDateValue method readDates.

/**
 * Extracts the dates from a JSON array.
 * @param array the JSON array where the dates are stored in.
 * @return list of the extracted dates.
 */
private SortedSet<Date> readDates(JSONArray array) {
    if (null != array) {
        SortedSet<Date> result = new TreeSet<>();
        for (int i = 0; i < array.length(); i++) {
            try {
                long l = Long.valueOf(array.getString(i)).longValue();
                result.add(new Date(l));
            } catch (NumberFormatException | JSONException e) {
                LOG.error("Could not read date from JSON array.", e);
            }
        }
        return result;
    }
    return new TreeSet<>();
}
Also used : TreeSet(java.util.TreeSet) JSONException(org.opencms.json.JSONException) Date(java.util.Date)

Example 5 with JSONException

use of org.opencms.json.JSONException in project opencms-core by alkacon.

the class CmsSerialDateValue method toJson.

/**
 * Convert the information from the wrapper to a JSON object.
 * @return the serial date information as JSON.
 */
public JSONObject toJson() {
    try {
        JSONObject result = new JSONObject();
        if (null != getStart()) {
            result.put(JsonKey.START, dateToJson(getStart()));
        }
        if (null != getEnd()) {
            result.put(JsonKey.END, dateToJson(getEnd()));
        }
        if (isWholeDay()) {
            result.put(JsonKey.WHOLE_DAY, true);
        }
        JSONObject pattern = patternToJson();
        result.put(JsonKey.PATTERN, pattern);
        SortedSet<Date> exceptions = getExceptions();
        if (!exceptions.isEmpty()) {
            result.put(JsonKey.EXCEPTIONS, datesToJson(exceptions));
        }
        switch(getEndType()) {
            case DATE:
                result.put(JsonKey.SERIES_ENDDATE, dateToJson(getSeriesEndDate()));
                break;
            case TIMES:
                result.put(JsonKey.SERIES_OCCURRENCES, String.valueOf(getOccurrences()));
                break;
            case SINGLE:
            default:
                break;
        }
        if (!isCurrentTillEnd()) {
            result.put(JsonKey.CURRENT_TILL_END, false);
        }
        if (null != getParentSeriesId()) {
            result.put(JsonKey.PARENT_SERIES, getParentSeriesId().getStringValue());
        }
        return result;
    } catch (JSONException e) {
        LOG.error("Could not convert Serial date value to JSON.", e);
        return null;
    }
}
Also used : JSONObject(org.opencms.json.JSONObject) JSONException(org.opencms.json.JSONException) Date(java.util.Date)

Aggregations

JSONException (org.opencms.json.JSONException)60 JSONObject (org.opencms.json.JSONObject)44 JSONArray (org.opencms.json.JSONArray)21 CmsException (org.opencms.main.CmsException)13 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)5 HashMap (java.util.HashMap)4 JspWriter (javax.servlet.jsp.JspWriter)4 CmsObject (org.opencms.file.CmsObject)4 CmsResource (org.opencms.file.CmsResource)4 CmsUser (org.opencms.file.CmsUser)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 Date (java.util.Date)2 Iterator (java.util.Iterator)2 Locale (java.util.Locale)2 Properties (java.util.Properties)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)2