View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.rest.web;
    21
    22  import java.net.URI;
    23  import java.net.URISyntaxException;
    24  import java.util.Map;
    25
    26  import org.neo4j.server.database.Database;
    27  import org.neo4j.server.database.DatabaseBlockedException;
    28  import org.neo4j.server.rest.domain.GraphDbHelper;
    29
    30  public class WebHelper {
    31      private final URI baseUri;
    32      private GraphDbHelper helper;
    33
    34      public WebHelper(URI baseUri, Database database) {
    35          this.baseUri = baseUri;
    36          this.helper = new GraphDbHelper(database);
    37
    38      }
    39
    40      public URI createNode() throws DatabaseBlockedException {
    41          long nodeId = helper.createNode();
    42          try {
    43              return new URI(baseUri.toString() + "/" + nodeId);
    44          } catch (URISyntaxException e) {
    45              throw new RuntimeException(e);
    46          }
    47      }
    48
    49      public URI createNodeWithProperties(Map<String, Object> props) throws DatabaseBlockedException {
    50          URI nodeUri = createNode();
    51          setNodeProperties(nodeUri, props);
    52          return nodeUri;
    53      }
    54
    55      private void setNodeProperties(URI nodeUri, Map<String, Object> props) throws DatabaseBlockedException {
    56          helper.setNodeProperties(extractNodeId(nodeUri), props);
    57      }
    58
    59      private long extractNodeId(URI nodeUri) {
    60          String path = nodeUri.getPath();
    61          if (path.startsWith("/")) {
    62              path = path.substring(1);
    63          }
    64
    65          return Long.parseLong(path);
    66      }
    67  }