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 org.neo4j.server.rest.domain.JsonHelper;
23  
24  import java.net.URI;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * Helps generate testable data models, using a RestfulGraphDatabase.
30   *
31   */
32  public class RestfulModelHelper
33  {
34      public static DomainModel generateMatrix( RestfulGraphDatabase rgd ) {
35          String key = "key_get";
36          String value = "value";
37  
38          DomainModel dm = new DomainModel();
39  
40          DomainEntity thomas = new DomainEntity();
41          thomas.properties.put("name", "Thomas Anderson");
42          thomas.location = (URI) rgd.createNode( "{\"name\":\"" + "Thomas Anderson" + "\"}" ).getMetadata().getFirst(
43                  "Location" );
44          dm.add(thomas);
45  
46          DomainEntity agent = new DomainEntity();
47          agent.properties.put("name", "Agent Smith");
48          agent.location = (URI) rgd.createNode( "{\"name\":\"" + "Agent Smith" + "\"}" ).getMetadata().getFirst(
49                  "Location" );
50          dm.add(agent);
51  
52          dm.nodeIndexName = "matrixal-nodes";
53          dm.indexedNodeKeyValues.put(key, value);
54          dm.indexedNodeUriToEntityMap.put( (URI) rgd.addToNodeIndex( dm.nodeIndexName, key, value,
55                  JsonHelper.createJsonFrom( thomas.location.toString() ) ).getMetadata().getFirst(
56                  "Location" ), thomas );
57          dm.indexedNodeUriToEntityMap.put( (URI) rgd.addToNodeIndex( dm.nodeIndexName, key, value,
58                  JsonHelper.createJsonFrom( agent.location.toString() ) ).getMetadata().getFirst(
59                  "Location" ), agent);
60  
61          return dm;
62      }
63  
64      public static class DomainEntity
65      {
66          public URI location;
67          public Map<String,String> properties = new HashMap<String, String>();
68      }
69  
70      public static class DomainModel {
71          public Map<URI, DomainEntity> nodeUriToEntityMap = new HashMap<URI, DomainEntity>();
72          String nodeIndexName = "nodes";
73          public Map<String,String> indexedNodeKeyValues = new HashMap<String, String>();
74          public Map<URI, DomainEntity> indexedNodeUriToEntityMap = new HashMap<URI, DomainEntity>();
75          String relationshipIndexName = "relationships";
76          public Map<URI, DomainEntity> indexedRelationshipUriToEntityMap = new HashMap<URI, DomainEntity>();
77          public Map<String,String> indexedRelationshipKeyValues = new HashMap<String, String>();
78  
79          public void add(DomainEntity de)
80          {
81              nodeUriToEntityMap.put(de.location, de);
82          }
83      }
84  }