private JSONValue toJson() {
   final JSONObject data = null == restoreddata ? new JSONObject() : restoreddata;
   try {
     data.put("historyversion", new JSONString(SearchPanelModel.historyversion));
     data.put("selectedtype", new JSONString(selectedCriterion.displaytype));
     final Iterator<String> iter = criteria.keySet().iterator();
     while (iter.hasNext()) {
       final String searchTypeKey = iter.next();
       if (!selectedCriterionHistory.containsKey(searchTypeKey)) {
         continue;
       }
       final JSONObject searchdata = new JSONObject();
       final JSONObject userdata = new JSONObject();
       searchdata.put(Context.get().getChallengeLogin().toLowerCase(), userdata);
       userdata.put(
           "selectedcriterion",
           new JSONString(selectedCriterionHistory.get(searchTypeKey).getName()));
       final JSONObject criteriadata = new JSONObject();
       for (final Criterion criterion : criteria.get(searchTypeKey).values()) {
         final String s = CriterionHistory.Writer.toJson(criterion.getHistory());
         criteriadata.put(criterion.getName(), JSONParser.parseStrict(s));
       }
       userdata.put("criteria", criteriadata);
       if (searchhistory.containsKey(searchTypeKey)) {
         userdata.put(
             "searchhistory",
             JSONParser.parseStrict(
                 SearchHistory.Writer.toJson(searchhistory.get(searchTypeKey))));
       }
       if (accesshistory.containsKey(searchTypeKey)) {
         userdata.put(
             "accesshistory",
             JSONParser.parseStrict(
                 AccessHistory.Writer.toJson(accesshistory.get(searchTypeKey))));
       }
       data.put(selectedCriterionHistory.get(searchTypeKey).getType().toLowerCase(), searchdata);
     }
   } catch (final Exception e) {
     getLogger().severe("SearchModel.toJson() : " + e);
   }
   return data;
 }
  private void fromJson(final JSONValue data) {
    try {
      if (null != data) {
        restoreddata = data.isObject();
        final String version = restoreddata.get("historyversion").isString().stringValue();
        if (!version.equals(SearchPanelModel.historyversion)) {
          throw new Exception("versionstring of search history has changed, need to reset history");
        }
        final String selectedtype = restoreddata.get("selectedtype").isString().stringValue();
        final Iterator<String> iter = criteria.keySet().iterator();
        while (iter.hasNext()) {
          final String searchTypeDisplayKey = iter.next();
          if (!restoreddata.containsKey(
              criteria.get(searchTypeDisplayKey).values().iterator().next().type)) {
            searchhistory.put(searchTypeDisplayKey, new SearchHistory());
            searchhistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.SEARCH_HISTORY_LIMIT);
            accesshistory.put(searchTypeDisplayKey, new AccessHistory());
            accesshistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.ACCESS_HISTORY_LIMIT);
            continue;
          }
          final String type = criteria.get(searchTypeDisplayKey).values().iterator().next().type;
          final JSONObject searchdata = restoreddata.get(type.toLowerCase()).isObject();
          final JSONObject userdata =
              searchdata.get(Context.get().getChallengeLogin().toLowerCase()).isObject();
          final Criterion criterion =
              criteria
                  .get(searchTypeDisplayKey)
                  .get(userdata.get("selectedcriterion").isString().stringValue());
          if (criterion.displaytype.equalsIgnoreCase(selectedtype)) {
            selectedCriterion = criterion;
          }
          selectedCriterionHistory.put(criterion.displaytype, criterion);
          final JSONObject criteriadata = userdata.get("criteria").isObject();
          for (final String key : criteriadata.keySet()) {
            criteria
                .get(criterion.displaytype)
                .get(key)
                .setHistory(criteriadata.get(key).isObject());
          }

          if (userdata.containsKey("searchhistory")) {
            searchhistory.put(
                searchTypeDisplayKey,
                SearchHistory.Reader.read(userdata.get("searchhistory").isObject()));
          } else {
            searchhistory.put(searchTypeDisplayKey, new SearchHistory());
            searchhistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.SEARCH_HISTORY_LIMIT);
          }
          if (userdata.containsKey("accesshistory")) {
            accesshistory.put(
                searchTypeDisplayKey,
                AccessHistory.Reader.read(userdata.get("accesshistory").isObject()));
          } else {
            accesshistory.put(searchTypeDisplayKey, new AccessHistory());
            accesshistory.get(searchTypeDisplayKey).setLimit(SearchPanelModel.ACCESS_HISTORY_LIMIT);
          }
        }
      }
    } catch (final Exception e) {
      getLogger().severe("SearchModel.fromJson() : " + e);
      reset();
    }
  }