View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.connector.store.jpa.model.simple;
25  
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.GeneratedValue;
29  import javax.persistence.GenerationType;
30  import javax.persistence.Id;
31  import javax.persistence.NamedQueries;
32  import javax.persistence.NamedQuery;
33  import javax.persistence.Table;
34  import org.hibernate.annotations.Index;
35  
36  /**
37   * Represents a single node that appears in a subgraph.
38   * 
39   * @see SubgraphQueryEntity
40   */
41  @Entity
42  @org.hibernate.annotations.Table( appliesTo = "MODE_SUBGRAPH_NODES", indexes = @Index( name = "QUERYID_INX", columnNames = {
43      "QUERY_ID", "UUID", "DEPTH"} ) )
44  @Table( name = "MODE_SUBGRAPH_NODES" )
45  @NamedQueries( {
46      @NamedQuery( name = "SubgraphNodeEntity.insertChildren", query = "insert into SubgraphNodeEntity(queryId,nodeUuid,depth,parentIndexInParent,indexInParent) select parentNode.queryId, child.nodeUuidString, parentNode.depth+1, parentNode.indexInParent, child.indexInParent from NodeEntity child, SubgraphNodeEntity parentNode where child.workspaceId = :workspaceId and child.parent.nodeUuidString = parentNode.nodeUuid and parentNode.queryId = :queryId and parentNode.depth = :parentDepth" ),
47      @NamedQuery( name = "SubgraphNodeEntity.getCount", query = "select count(*) from SubgraphNodeEntity where queryId = :queryId" ),
48      @NamedQuery( name = "SubgraphNodeEntity.getNodeEntitiesWithLargeValues", query = "select props from NodeEntity props, SubgraphNodeEntity node where props.workspaceId = :workspaceId and props.nodeUuidString = node.nodeUuid and node.queryId = :queryId and node.depth >= :depth and size(props.largeValues) > 0" ),
49      @NamedQuery( name = "SubgraphNodeEntity.getChildEntities", query = "select child from NodeEntity child, SubgraphNodeEntity node where child.workspaceId = :workspaceId and child.nodeUuidString = node.nodeUuid and node.queryId = :queryId and node.depth >= :depth and node.depth <= :maxDepth order by node.depth, node.parentIndexInParent, node.indexInParent" ),
50      // @NamedQuery( name = "SubgraphNodeEntity.getInternalReferences", query =
51      // "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId) and ref.id.fromUuidString in (select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId)"
52      // ),
53      // @NamedQuery( name = "SubgraphNodeEntity.getOutwardReferences", query =
54      // "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString not in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId) and ref.id.fromUuidString in (select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId)"
55      // ),
56      // @NamedQuery( name = "SubgraphNodeEntity.getInwardReferences", query =
57      // "select ref from ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.toUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId) and ref.id.fromUuidString not in (select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId)"
58      // ),
59      @NamedQuery( name = "SubgraphNodeEntity.clearParentReferences", query = "update NodeEntity child set child.parent = null where child.workspaceId = :workspaceId and child.nodeUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId and node.depth >= :depth )" ),
60      @NamedQuery( name = "SubgraphNodeEntity.deleteChildEntities", query = "delete NodeEntity child where child.workspaceId = :workspaceId and child.nodeUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId and node.depth >= :depth )" ),
61      // @NamedQuery( name = "SubgraphNodeEntity.deleteReferences", query =
62      // "delete ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.fromUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId )"
63      // ),
64      @NamedQuery( name = "SubgraphNodeEntity.deleteByQueryId", query = "delete SubgraphNodeEntity where queryId = :queryId" )} )
65  public class SubgraphNodeEntity {
66  
67      @Id
68      @Column( name = "ID" )
69      @GeneratedValue( strategy = GenerationType.AUTO )
70      private Long id;
71  
72      @Column( name = "QUERY_ID", nullable = false, unique = false, updatable = false )
73      private Long queryId;
74  
75      @Column( name = "UUID", updatable = false, nullable = false, length = 36 )
76      private String nodeUuid;
77  
78      @Column( name = "DEPTH", updatable = false, nullable = false )
79      private int depth;
80  
81      @Column( name = "PARENT_NUM", updatable = false, nullable = false )
82      private int parentIndexInParent;
83  
84      @Column( name = "CHILD_NUM", updatable = false, nullable = false )
85      private int indexInParent;
86  
87      public SubgraphNodeEntity() {
88      }
89  
90      public SubgraphNodeEntity( Long queryId,
91                                 String nodeUuid,
92                                 int depth ) {
93          this.queryId = queryId;
94          this.nodeUuid = nodeUuid;
95          this.depth = depth;
96      }
97  
98      /**
99       * @return id
100      */
101     public Long getId() {
102         return id;
103     }
104 
105     /**
106      * @return depth
107      */
108     public int getDepth() {
109         return depth;
110     }
111 
112     /**
113      * @return nodeUuid
114      */
115     public String getNodeUuid() {
116         return nodeUuid;
117     }
118 
119     /**
120      * @return queryId
121      */
122     public Long getQueryId() {
123         return queryId;
124     }
125 
126     /**
127      * @return indexInParent
128      */
129     public int getIndexInParent() {
130         return indexInParent;
131     }
132 
133     /**
134      * @return parentIndexInParent
135      */
136     public int getParentIndexInParent() {
137         return parentIndexInParent;
138     }
139 
140     /**
141      * {@inheritDoc}
142      * 
143      * @see java.lang.Object#hashCode()
144      */
145     @Override
146     public int hashCode() {
147         return id != null ? id.intValue() : 0;
148     }
149 
150     /**
151      * {@inheritDoc}
152      * 
153      * @see java.lang.Object#equals(java.lang.Object)
154      */
155     @Override
156     public boolean equals( Object obj ) {
157         if (obj == this) return true;
158         if (obj instanceof SubgraphNodeEntity) {
159             SubgraphNodeEntity that = (SubgraphNodeEntity)obj;
160             if (this.id.equals(that.id)) return true;
161         }
162         return false;
163     }
164 
165     /**
166      * {@inheritDoc}
167      * 
168      * @see java.lang.Object#toString()
169      */
170     @Override
171     public String toString() {
172         return "" + id + " - Query " + queryId + "; depth=" + depth + "; node=" + nodeUuid + " at index " + indexInParent;
173     }
174 
175 }