Editor tracking

Download Sample Viewer

Description

This sample shows how to make use of the EditFieldsInfo from a feature service which has editor tracking capabilities. A feature layer is created from a feature service table, then added to a map. The EditFieldsInfo object is then obtained from the initialized feature service table. In this application, the EditFieldsInfo fields are displayed to the user as a MapTip.

A user must log in to use the service, which is done via a UserCredentials instance passed in the GeodatabaseFeatureServiceTable constructor. The sample allows adding new features and updating an existing feature's attributes, if allowed. You may only be allowed to update features that you created, depending on the Ownership Based Access Control (OBAC) settings for the service layer.

To update a feature's attributes, click the 'View/Edit' button and click on the feature you would like to edit. An editable popup view will be shown if the logged in user is allowed to update the feature. If the user is not allowed to update the feature (because another user created the feature and the OBAC settings don't allow others to update someone's features), a non-editable popup view will be shown instead.

To add a feature, click on the 'Add feature' button and choose a feature template from the template picker control. The added feature will store the creation time and creator in the appropriate fields.

Code snippet


    featureLayer = new FeatureLayer(featureServiceTable);
    featureLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        // get the EditFieldsInfo from the feature service table
        EditFieldsInfo efi = featureServiceTable.getEditFieldsInfo();
        // create a MapTip to display all the EditFieldsInfo fields
        MapTip mapTip = new MapTip();
        LinkedHashMap<String, String> displayFields = new LinkedHashMap<>();
        displayFields.put(efi.getCreatorField(), "Creator: ");
        displayFields.put(efi.getCreationDateField(), "Creation date: ");
        displayFields.put(efi.getEditorField(), "Last editor: ");
        displayFields.put(efi.getEditDateField(), "Last edit date: ");
        mapTip.setDisplayFields(displayFields);
        featureLayer.setMapTip(mapTip);
      }
    });
  

Sample Code

/* Copyright 2014 Esri

All rights reserved under the copyright laws of the United States
and applicable international laws, treaties, and conventions.

You may freely redistribute and use this sample code, with or
without modification, provided you include the original copyright
notice and use restrictions.

See the use restrictions.*/
package com.esri.client.samples.editing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.LinkedHashMap;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import com.esri.core.geodatabase.GeodatabaseFeatureServiceTable;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.EditFieldsInfo;
import com.esri.core.map.Feature;
import com.esri.map.ArcGISTiledMapServiceLayer;
import com.esri.map.FeatureLayer;
import com.esri.map.JMap;
import com.esri.map.LayerInitializeCompleteEvent;
import com.esri.map.LayerInitializeCompleteListener;
import com.esri.map.MapTip;
import com.esri.map.popup.PopupDialog;
import com.esri.map.popup.PopupView;
import com.esri.map.popup.PopupViewEvent;
import com.esri.map.popup.PopupViewListener;
import com.esri.toolkit.editing.JTemplatePicker;
import com.esri.toolkit.overlays.HitTestEvent;
import com.esri.toolkit.overlays.HitTestListener;
import com.esri.toolkit.overlays.HitTestOverlay;

/***
 * This sample shows how to make use of the EditFieldsInfo from a
 * feature service which has editor tracking capabilities. A feature
 * layer is created from a feature service table, then added to a 
 * map. The EditFieldsInfo object is then obtained from the initialized 
 * feature service table. In this application, the EditFieldsInfo fields 
 * are displayed to the user as a {@link MapTip}.
 * <p>
 * A user must log in to use the service, which is done via a UserCredentials
 * instance passed in the GeodatabaseFeatureServiceTable constructor. 
 * The sample allows adding new features and updating an existing feature's 
 * attributes, if allowed. You may only be allowed to update features 
 * that you created, depending on the Ownership Based Access Control (OBAC) 
 * settings for the service layer.
 * <p>
 * To update a feature's attributes, click the 'View/Edit' button and click 
 * on the feature you would like to edit. An editable popup view will be 
 * shown if the logged in user is allowed to update the feature. If the user 
 * is not allowed to update the feature (because another user created the 
 * feature and the OBAC settings don't allow others to update someone's 
 * features), a non-editable popup view will be shown instead.
 * <p>
 * To add a feature, click on the 'Add feature' button and choose a feature 
 * template from the template picker control. The added feature will store 
 * the creation time and creator in the appropriate fields.
 */
public class EditorTrackingApp {

  private JMap map;
  private JComponent contentPane;
  private JTemplatePicker templatePicker;
  private HitTestOverlay hitTestOverlay;
  private GeodatabaseFeatureServiceTable featureServiceTable;
  private FeatureLayer featureLayer;
  // example user credentials specific to this sample service
  private final static String USERNAME = "USER1";
  private final static String PASSWORD = "user1";

  public EditorTrackingApp() { }

  // ------------------------------------------------------------------------
  // Core Functionality
  // ------------------------------------------------------------------------
  /**
   * Creates a map with a tiled layer and a feature layer. A feature service 
   * table is created using the feature service URL, layer ID, and credentials 
   * for the service. The credentials are then used internally to populate the 
   * creator and editor fields for any feature edits made. The feature data 
   * is then displayed in a feature layer created from the feature service 
   * table. An overlay to handle mouse clicks is also added to the map.
   *  
   * @return a map.
   */
  private JMap createMap() throws Exception {
    final JMap jMap = new JMap();

    final UserCredentials credentials = new UserCredentials();
    credentials.setUserAccount(USERNAME, PASSWORD);

    ArcGISTiledMapServiceLayer tiled = new ArcGISTiledMapServiceLayer(
        "http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
    jMap.getLayers().add(tiled);

    featureServiceTable = new GeodatabaseFeatureServiceTable(
        "http://sampleserver6.arcgisonline.com/arcgis/rest/services/SaveTheBay/FeatureServer", 
        credentials, 
        0);
    // set the table's spatial reference to match the map's, must be done before table initialization
    featureServiceTable.setSpatialReference(
        SpatialReference.create(SpatialReference.WKID_WGS84_WEB_MERCATOR_AUXILIARY_SPHERE));
    featureServiceTable.initialize(); // using synchronous version for simplicity

    featureLayer = new FeatureLayer(featureServiceTable);
    featureLayer.addLayerInitializeCompleteListener(new LayerInitializeCompleteListener() {

      @Override
      public void layerInitializeComplete(LayerInitializeCompleteEvent e) {
        // get the EditFieldsInfo from the feature service table
        final EditFieldsInfo efi = featureServiceTable.getEditFieldsInfo();
        // create a MapTip to display all the EditFieldsInfo fields
        MapTip mapTip = new MapTip();
        LinkedHashMap<String, String> displayFields = new LinkedHashMap<>();
        displayFields.put(efi.getCreatorField(), "Creator: ");
        displayFields.put(efi.getCreationDateField(), "Creation date: ");
        displayFields.put(efi.getEditorField(), "Last editor: ");
        displayFields.put(efi.getEditDateField(), "Last edit date: ");
        mapTip.setDisplayFields(displayFields);
        featureLayer.setMapTip(mapTip);
      }
    });
    jMap.getLayers().add(featureLayer);

    jMap.setExtent(new Envelope(-10610907, 3373622, -10581666, 3393126));

    hitTestOverlay = new HitTestOverlay(featureLayer);
    hitTestOverlay.addHitTestListener(new HitTestListener() {
      @Override
      public void featureHit(HitTestEvent event) {
        HitTestOverlay overlay = event.getOverlay();
        // get first (top-most) feature hit by the mouse
        Feature hitFeature = overlay.getHitFeatures().get(0);

        if (featureServiceTable.canUpdate(hitFeature.getId())) {
          // user allowed to update this feature: create an editing popup view
          PopupView popupView = PopupView.createEditView("Editing feature " + hitFeature.getId(), featureLayer);
          popupView.setFeature(jMap, featureServiceTable, hitFeature);
          // create a popup dialog passing in the editing popup view
          final PopupDialog popup = jMap.createPopup(
              new JComponent[]{popupView}, hitFeature);
          popup.setTitle("Edit Attributes");
          popup.setVisible(true);
          popupView.addPopupViewListener(new PopupViewListener() {

            @Override
            public void onCommitEdit(PopupViewEvent popupViewEvent, Feature feature) {
              popup.close();
            }

            @Override
            public void onCancelEdit(PopupViewEvent popupViewEvent, Feature feature) {
              popup.close();
            }
          });
        } else {
          // user not allowed to update this feature: show attributes view (not editable)
          PopupView popupView = PopupView.createAttributesView("Viewing feature " + hitFeature.getId(), hitFeature);
          popupView.setFeature(jMap, featureServiceTable, hitFeature);
          // create a popup dialog passing in the attributes popup view
          final PopupDialog popup = jMap.createPopup(
              new JComponent[]{popupView}, hitFeature);
          popup.setTitle("View Attributes");
          popup.setVisible(true);
        }
      }
    });
    hitTestOverlay.setActive(false);
    jMap.addMapOverlay(hitTestOverlay);

    return jMap;
  }

  // ------------------------------------------------------------------------
  // Public methods
  // ------------------------------------------------------------------------
  /**
   * Creates and displays the UI, including the map, for this application.
   */
  public JComponent createUI() throws Exception {

    contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());

    map = createMap();
    templatePicker = createTemplatePicker(map);
    JToolBar toolBar = createToolBar();

    contentPane.add(templatePicker, BorderLayout.WEST);
    contentPane.add(toolBar, BorderLayout.NORTH);
    contentPane.add(map, BorderLayout.CENTER);

    return contentPane;
  }

  // ------------------------------------------------------------------------
  // Static methods
  // ------------------------------------------------------------------------
  /**
   * Starting point of this application.
   * @param args arguments to this application.
   */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        try {
          // instance of this application
          EditorTrackingApp featureDataFormApp = new EditorTrackingApp();

          // create the UI, including the map, for the application.
          JFrame appWindow = featureDataFormApp.createWindow();
          appWindow.add(featureDataFormApp.createUI());
          appWindow.setVisible(true);
        } catch (Exception e) {
          // on any error, display the stack trace.
          e.printStackTrace();
        }
      }
    });
  }

  // ------------------------------------------------------------------------
  // Private methods
  // ------------------------------------------------------------------------
  private JToolBar createToolBar() {

    JToolBar toolBar = new JToolBar();
    toolBar.setLayout(new FlowLayout(FlowLayout.CENTER));

    JLabel label = new JLabel("You are logged in as: "+USERNAME+".   ");
    toolBar.add(label);

    JButton addButton = new JButton("Add a feature");
    addButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        templatePicker.setEnabled(true);
        hitTestOverlay.setActive(false);
      }
    });
    toolBar.add(addButton);

    JButton editButton = new JButton("View/Edit a feature's attributes");
    editButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        templatePicker.setEnabled(false);
        hitTestOverlay.setActive(true);
      }
    });
    toolBar.add(editButton);

    contentPane.add(toolBar, BorderLayout.NORTH);

    return toolBar;
  }

  private JTemplatePicker createTemplatePicker(JMap jMap) {
    JTemplatePicker tp = new JTemplatePicker(jMap);
    tp.addLayer(featureLayer);
    tp.setEnabled(false);
    return tp;
  }

  /**
   * Creates the application window.
   * @return a window.
   */
  private JFrame createWindow() {
    JFrame window = new JFrame();
    window.setBounds(100, 100, 1000, 700);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(new BorderLayout(0, 0));
    window.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent windowEvent) {
        super.windowClosing(windowEvent);
        if (map != null) map.dispose();
        if (featureServiceTable != null) featureServiceTable.dispose();
      }
    });
    return window;
  }
}
Feedback on this topic?