1   /**
2    * Licensed to Neo Technology under one or more contributor
3    * license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright
5    * ownership. Neo Technology licenses this file to you under
6    * the Apache License, Version 2.0 (the "License"); you may
7    * not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.neo4j.examples.server;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  public class TraversalDescription {
26  
27      public static final String DEPTH_FIRST = "depth first";
28      public static final String NODE = "node";
29      public static final String ALL = "all";
30  
31      private String uniqueness = NODE;
32      private int maxDepth = 1;
33      private String returnFilter = ALL;
34      private String order = DEPTH_FIRST;
35      private List<Relationship> relationships = new ArrayList<Relationship>();
36  
37      public void setOrder(String order) {
38          this.order = order;
39      }
40  
41      public void setUniqueness(String uniqueness) {
42          this.uniqueness = uniqueness;
43      }
44  
45      public void setMaxDepth(int maxDepth) {
46          this.maxDepth = maxDepth;
47      }
48  
49      public void setReturnFilter(String returnFilter) {
50          this.returnFilter = returnFilter;
51      }
52  
53      public void setRelationships(Relationship... relationships) {
54          this.relationships =  Arrays.asList(relationships);
55      }
56  
57      public String toJson() {
58          StringBuilder sb = new StringBuilder();
59          sb.append("{ ");
60          sb.append(" \"order\" : \"" + order + "\"");
61          sb.append(", ");
62          sb.append(" \"uniqueness\" : \"" + uniqueness + "\"");
63          sb.append(", ");
64          if (relationships.size() > 0) {
65              sb.append("\"relationships\" : [");
66              for (int i = 0; i < relationships.size(); i++) {
67                  sb.append(relationships.get(i).toJsonCollection());
68                  if (i < relationships.size() - 1) { // Miss off the final comma
69                      sb.append(", ");
70                  }
71              }
72              sb.append("], ");
73          }
74          sb.append("\"return filter\" : { ");
75          sb.append("\"language\" : \"builtin\", ");
76          sb.append("\"name\" : \"");
77          sb.append(returnFilter);
78          sb.append("\" }, ");
79          sb.append("\"max depth\" : ");
80          sb.append(maxDepth);
81          sb.append(" }");
82          return sb.toString();
83      }
84  }