001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.connector.store.jpa.model.basic;
025    
026    import javax.persistence.Column;
027    import javax.persistence.Entity;
028    import javax.persistence.GeneratedValue;
029    import javax.persistence.GenerationType;
030    import javax.persistence.Id;
031    import javax.persistence.NamedQueries;
032    import javax.persistence.NamedQuery;
033    import javax.persistence.Table;
034    import org.hibernate.annotations.Index;
035    
036    /**
037     * Represents a single node that appears in a subgraph.
038     * 
039     * @author Randall Hauch
040     * @see SubgraphQueryEntity
041     */
042    @Entity
043    @Table( name = "DNA_SUBGRAPH_NODES" )
044    @org.hibernate.annotations.Table( appliesTo = "DNA_SUBGRAPH_NODES", indexes = @Index( name = "QUERYID_INX", columnNames = {
045        "QUERY_ID", "UUID"} ) )
046    @NamedQueries( {
047        @NamedQuery( name = "SubgraphNodeEntity.insertChildren", query = "insert into SubgraphNodeEntity(queryId,nodeUuid,depth,parentIndexInParent,indexInParent) select parentNode.queryId, child.id.childUuidString, parentNode.depth+1, parentNode.indexInParent, child.indexInParent from ChildEntity child, SubgraphNodeEntity parentNode where child.id.workspaceId = :workspaceId and child.id.parentUuidString = parentNode.nodeUuid and parentNode.queryId = :queryId and parentNode.depth = :parentDepth" ),
048        @NamedQuery( name = "SubgraphNodeEntity.getCount", query = "select count(*) from SubgraphNodeEntity where queryId = :queryId" ),
049        @NamedQuery( name = "SubgraphNodeEntity.getPropertiesEntities", query = "select props from PropertiesEntity props, SubgraphNodeEntity node where props.id.workspaceId = :workspaceId and props.id.uuidString = node.nodeUuid and node.queryId = :queryId and node.depth >= :depth and node.depth <= :maxDepth order by node.depth, node.parentIndexInParent, node.indexInParent" ),
050        @NamedQuery( name = "SubgraphNodeEntity.getPropertiesEntitiesWithLargeValues", query = "select props from PropertiesEntity props, SubgraphNodeEntity node where props.id.workspaceId = :workspaceId and props.id.uuidString = node.nodeUuid and node.queryId = :queryId and node.depth >= :depth and size(props.largeValues) > 0" ),
051        @NamedQuery( name = "SubgraphNodeEntity.getChildEntities", query = "select child from ChildEntity child, SubgraphNodeEntity node where child.id.workspaceId = :workspaceId and child.id.childUuidString = node.nodeUuid and node.queryId = :queryId and node.depth >= :depth and node.depth <= :maxDepth order by node.depth, node.parentIndexInParent, node.indexInParent" ),
052        @NamedQuery( name = "SubgraphNodeEntity.getInternalReferences", query = "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)" ),
053        @NamedQuery( name = "SubgraphNodeEntity.getOutwardReferences", query = "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)" ),
054        @NamedQuery( name = "SubgraphNodeEntity.getInwardReferences", query = "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)" ),
055        @NamedQuery( name = "SubgraphNodeEntity.deletePropertiesEntities", query = "delete PropertiesEntity props where props.id.workspaceId = :workspaceId and props.id.uuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId )" ),
056        @NamedQuery( name = "SubgraphNodeEntity.deleteChildEntities", query = "delete ChildEntity child where child.id.workspaceId = :workspaceId and child.id.childUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId )" ),
057        @NamedQuery( name = "SubgraphNodeEntity.deleteReferences", query = "delete ReferenceEntity as ref where ref.id.workspaceId = :workspaceId and ref.id.fromUuidString in ( select node.nodeUuid from SubgraphNodeEntity node where node.queryId = :queryId )" ),
058        @NamedQuery( name = "SubgraphNodeEntity.deleteByQueryId", query = "delete SubgraphNodeEntity where queryId = :queryId" )} )
059    public class SubgraphNodeEntity {
060    
061        @Id
062        @Column( name = "ID" )
063        @GeneratedValue( strategy = GenerationType.AUTO )
064        private Long id;
065    
066        @Column( name = "QUERY_ID", nullable = false, unique = false, updatable = false )
067        private Long queryId;
068    
069        @Column( name = "UUID", updatable = false, nullable = false, length = 36 )
070        private String nodeUuid;
071    
072        @Column( name = "DEPTH", updatable = false, nullable = false )
073        private int depth;
074    
075        @Column( name = "PARENT_NUM", updatable = false, nullable = false )
076        private int parentIndexInParent;
077    
078        @Column( name = "CHILD_NUM", updatable = false, nullable = false )
079        private int indexInParent;
080    
081        public SubgraphNodeEntity() {
082        }
083    
084        public SubgraphNodeEntity( Long queryId,
085                                   String nodeUuid,
086                                   int depth ) {
087            this.queryId = queryId;
088            this.nodeUuid = nodeUuid;
089            this.depth = depth;
090        }
091    
092        /**
093         * @return id
094         */
095        public Long getId() {
096            return id;
097        }
098    
099        /**
100         * @return depth
101         */
102        public int getDepth() {
103            return depth;
104        }
105    
106        /**
107         * @return nodeUuid
108         */
109        public String getNodeUuid() {
110            return nodeUuid;
111        }
112    
113        /**
114         * @return queryId
115         */
116        public Long getQueryId() {
117            return queryId;
118        }
119    
120        /**
121         * @return indexInParent
122         */
123        public int getIndexInParent() {
124            return indexInParent;
125        }
126    
127        /**
128         * @return parentIndexInParent
129         */
130        public int getParentIndexInParent() {
131            return parentIndexInParent;
132        }
133    
134        /**
135         * {@inheritDoc}
136         * 
137         * @see java.lang.Object#hashCode()
138         */
139        @Override
140        public int hashCode() {
141            return id != null ? id.intValue() : 0;
142        }
143    
144        /**
145         * {@inheritDoc}
146         * 
147         * @see java.lang.Object#equals(java.lang.Object)
148         */
149        @Override
150        public boolean equals( Object obj ) {
151            if (obj == this) return true;
152            if (obj instanceof SubgraphNodeEntity) {
153                SubgraphNodeEntity that = (SubgraphNodeEntity)obj;
154                if (this.id.equals(that.id)) return true;
155            }
156            return false;
157        }
158    
159        /**
160         * {@inheritDoc}
161         * 
162         * @see java.lang.Object#toString()
163         */
164        @Override
165        public String toString() {
166            return "" + id + " - Query " + queryId + "; depth=" + depth + "; node=" + nodeUuid + " at index " + indexInParent;
167        }
168    
169    }