public static JSONArray getStartupSequence(final View view) throws ViewException {
   final JSONArray startupSequence = new JSONArray();
   final List<Bundle> bundles = view.getBundles();
   log.debug("Got", bundles.size(), "states for view", view.getId());
   for (Bundle s : bundles) {
     final String startup = s.getStartup();
     final String name = s.getName();
     if (startup != null) {
       try {
         startupSequence.put(new JSONObject(startup));
       } catch (JSONException jsonex) {
         log.error(
             jsonex,
             "Malformed JSON in startup sequence fragment for bundle:",
             name,
             "- JSON:",
             startup);
       }
     } else {
       throw new ViewException(
           "Could not get startup sequence fragment for bundle '" + name + "'");
     }
   }
   return startupSequence;
 }
 private View saveView(final View view) {
   try {
     if (view.getId() != -1) {
       viewService.updatePublishedView(view);
     } else {
       long viewId = viewService.addView(view);
       view.setId(viewId);
     }
   } catch (ViewException e) {
     log.error("Error when trying add/update published view", e);
   }
   return view;
 }
 private Bundle addBundle(final View view, final String bundleid) {
   Bundle bundle = view.getBundleByName(bundleid);
   if (bundle == null) {
     log.info("Bundle with id:", bundleid, "not found in currentView - adding");
     if (!bundleCache.containsKey(bundleid)) {
       log.warn("Trying to add bundle that isn't loaded:", bundleid, "- Skipping it!");
       return null;
     }
     bundle = bundleCache.get(bundleid).clone();
     view.addBundle(bundle);
   }
   return bundle;
 }
  public static JSONObject getConfiguration(final View view) throws ViewException {
    final JSONObject configuration = new JSONObject();
    final List<Bundle> bundles = view.getBundles();
    for (Bundle s : bundles) {
      final String conf = s.getConfig();
      final String state = s.getState();
      final String name = s.getBundleinstance();
      try {
        // setup bundle node in config
        JSONObject bundle = new JSONObject();
        configuration.put(name, bundle);

        // setup conf for bundle
        if (conf != null) {
          bundle.put("conf", new JSONObject(conf));
        } else {
          log.warn("Could not get configuration fragment for bundle '", name, "'");
        }
        // setup state for bundle
        if (state != null) {
          bundle.put("state", new JSONObject(state));
        } else {
          log.warn("Could not get state fragment for bundle '", name, "'");
        }
      } catch (Exception ex) {
        log.error("Malformed JSON in configuration fragment for bundle", name, conf);
      }
    }
    return configuration;
  }
  private void setupBundle(final View view, final JSONObject publisherData, final String bundleid) {

    final JSONObject bundleData = publisherData.optJSONObject(bundleid);
    if (bundleData != null && bundleData.names().length() > 0) {
      log.info("config found for", bundleid);
      final Bundle bundle = addBundle(view, bundleid);
      mergeBundleConfiguration(bundle, bundleData, null);
    } else {
      log.warn("config not found for", bundleid, "- removing bundle.");
      // We have to remove the bundle...
      // TODO: check if we really want to remove the bundle from view since it could be template
      // view???
      view.removeBundle(bundleid);
    }
  }
示例#6
0
  /**
   * Skips id, oldId and uuid but clones the rest of the info. Bundles retain ids.
   *
   * @return cloned object with bundles
   */
  public View cloneBasicInfo() {
    View view = new View();
    //        // skip id, oldId, uuid, isDefault
    // skip id, oldId, uuid
    view.setName(getName());
    view.setDescription(getDescription());
    view.setType(getType());
    view.setDevelopmentPath(getDevelopmentPath());
    view.setApplication(getApplication());
    view.setIsPublic(isPublic());
    view.setLang(getLang());
    view.setPage(getPage());
    view.setPubDomain(getPubDomain());
    view.setIsDefault(isDefault());
    for (Bundle bundle : getBundles()) {
      view.addBundle(bundle.clone());
    }

    return view;
  }
  private View getBaseView(final JSONObject publisherInput, final User user)
      throws ActionException {

    if (user.isGuest()) {
      throw new ActionDeniedException("Trying to publish map, but couldn't determine user");
    }

    // not editing, use template view
    if (PUBLISHED_VIEW_TEMPLATE_ID == -1) {
      log.error("Publish template id not configured (property: view.template.publish)!");
      throw new ActionParamsException("Trying to publish map, but template isn't configured");
    }
    log.debug("Using template to create a new view");
    // Get publisher defaults
    View templateView = viewService.getViewWithConf(PUBLISHED_VIEW_TEMPLATE_ID);
    if (templateView == null) {
      log.error("Could not get template View with id:", PUBLISHED_VIEW_TEMPLATE_ID);
      throw new ActionParamsException("Could not get template View");
    }

    // clone a blank view based on template (so template doesn't get updated!!)
    final View view = templateView.cloneBasicInfo();
    final long viewId = publisherInput.optLong("id", -1);
    if (viewId != -1) {
      // check loaded view against user if we are updating a view
      log.debug("Loading view for editing:", viewId);
      final View existingView = viewService.getViewWithConf(viewId);
      if (user.getId() != existingView.getCreator()) {
        throw new ActionDeniedException("No permissions to update view with id:" + viewId);
      }
      // setup ids for updating a view
      view.setId(existingView.getId());
      view.setSupplementId(existingView.getSupplementId());
      view.setUuid(existingView.getUuid());
      view.setOldId(existingView.getOldId());
    }

    return view;
  }
  public void handleAction(ActionParameters params) throws ActionException {

    final User user = params.getUser();

    // Parse stuff sent by JS
    final JSONObject publisherData = getPublisherInput(params.getRequiredParam(KEY_PUBDATA));
    final View currentView = getBaseView(publisherData, user);

    final Bundle mapFullBundle = currentView.getBundleByName(ViewModifier.BUNDLE_MAPFULL);
    if (mapFullBundle == null) {
      throw new ActionParamsException("Could find mapfull bundle from view:" + currentView.getId());
    }

    // Setup user
    try {
      JSONObject userJson = new JSONObject();
      userJson.put(KEY_FIRSTNAME, user.getFirstname());
      userJson.put(KEY_LASTNAME, user.getLastname());
      userJson.put(KEY_NICKNAME, user.getScreenname());
      userJson.put(KEY_LOGINNAME, user.getEmail());
      JSONHelper.putValue(mapFullBundle.getConfigJSON(), KEY_USER, userJson);
      // mapfullTemplateConfig.put(KEY_USER, userJson);
    } catch (JSONException jsonex) {
      log.error("Could not create user object:", user, "- Error:", jsonex.getMessage());
      throw new ActionParamsException("User data problem");
    }

    // setup basic info about view
    final String domain = JSONHelper.getStringFromJSON(publisherData, KEY_DOMAIN, null);
    if (domain == null) {
      throw new ActionParamsException("Domain missing");
    }
    final String name =
        JSONHelper.getStringFromJSON(
            publisherData, KEY_NAME, "Published map " + System.currentTimeMillis());
    final String language =
        JSONHelper.getStringFromJSON(
            publisherData, KEY_LANGUAGE, PropertyUtil.getDefaultLanguage());

    currentView.setPubDomain(domain);
    currentView.setName(name);
    currentView.setType(params.getHttpParam(ViewTypes.VIEW_TYPE, ViewTypes.PUBLISHED));
    currentView.setCreator(user.getId());
    currentView.setIsPublic(true);
    // application/page/developmentPath should be configured to publish template view
    currentView.setLang(language);

    // setup map state
    setupMapState(mapFullBundle, publisherData, user);

    // setup infobox
    final JSONObject tmpInfoboxState = publisherData.optJSONObject(ViewModifier.BUNDLE_INFOBOX);
    if (tmpInfoboxState != null) {
      final Bundle infoboxTemplateBundle = currentView.getBundleByName(ViewModifier.BUNDLE_INFOBOX);
      if (infoboxTemplateBundle != null) {
        infoboxTemplateBundle.setState(tmpInfoboxState.toString());
      } else {
        log.warn(
            "Publisher sent state for infobox, but infobox isn't available in template view! State:",
            tmpInfoboxState);
      }
    }

    // Setup publishedmyplaces2 bundle if user has configured it/has permission to do so
    if (user.hasAnyRoleIn(drawToolsEnabledRoles)) {
      setupBundle(currentView, publisherData, ViewModifier.BUNDLE_PUBLISHEDMYPLACES2);
    }

    // Setup toolbar bundle if user has configured it
    setupBundle(currentView, publisherData, ViewModifier.BUNDLE_TOOLBAR);

    // Setup thematic map/published grid bundle
    final JSONObject gridState = publisherData.optJSONObject(KEY_GRIDSTATE);
    log.debug("Grid state:", gridState);
    if (gridState != null) {
      final Bundle gridBundle = addBundle(currentView, ViewModifier.BUNDLE_PUBLISHEDGRID);
      log.debug("Grid bundle added:", gridBundle);
      mergeBundleConfiguration(gridBundle, null, gridState);
    }

    log.debug("Save view:", currentView);
    final View newView = saveView(currentView);
    log.debug("Published a map:", newView);

    try {
      JSONObject newViewJson = new JSONObject(newView.toString());
      ResponseHelper.writeResponse(params, newViewJson);
    } catch (JSONException je) {
      log.error(je, "Could not create JSON response.");
      ResponseHelper.writeResponse(params, false);
    }
  }