PageRenderTime 45ms CodeModel.GetById 11ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/com/google/appengine/datanucleus/test/OwnedJoinsJDO.java

http://datanucleus-appengine.googlecode.com/
Java | 222 lines | 159 code | 45 blank | 18 comment | 34 complexity | 5652ee9bc1067f6f0ea3ca365becc87a MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.test;
 17
 18import com.google.appengine.api.datastore.Key;
 19import com.google.appengine.datanucleus.Utils;
 20
 21
 22import java.util.ArrayList;
 23import java.util.Arrays;
 24import java.util.List;
 25
 26import javax.jdo.annotations.Element;
 27import javax.jdo.annotations.IdGeneratorStrategy;
 28import javax.jdo.annotations.PersistenceCapable;
 29import javax.jdo.annotations.Persistent;
 30import javax.jdo.annotations.PrimaryKey;
 31
 32/**
 33 * @author Max Ross <maxr@google.com>
 34 */
 35public class OwnedJoinsJDO {
 36
 37  @PersistenceCapable(detachable = "true")
 38  public static class Student {
 39    @PrimaryKey
 40    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 41    private Long id;
 42
 43    private int grade;
 44
 45    @Element(dependent = "true")
 46    private List<Course> courses = new ArrayList<Course>();
 47
 48    @Persistent
 49    private Major major;
 50
 51    public int getGrade() {
 52      return grade;
 53    }
 54
 55    public void setGrade(int grade) {
 56      this.grade = grade;
 57    }
 58
 59    public List<Course> getCourses() {
 60      return courses;
 61    }
 62
 63    public void setCourses(List<Course> courses) {
 64      this.courses = courses;
 65    }
 66
 67    public Major getMajor() {
 68      return major;
 69    }
 70
 71    public void setMajor(Major major) {
 72      this.major = major;
 73    }
 74
 75    @Override
 76    public boolean equals(Object o) {
 77      if (this == o) {
 78        return true;
 79      }
 80      if (o == null || getClass() != o.getClass()) {
 81        return false;
 82      }
 83
 84      Student student = (Student) o;
 85
 86      if (id != null ? !id.equals(student.id) : student.id != null) {
 87        return false;
 88      }
 89
 90      return true;
 91    }
 92
 93    @Override
 94    public int hashCode() {
 95      return id != null ? id.hashCode() : 0;
 96    }
 97  }
 98
 99  @PersistenceCapable(detachable = "true")
100  public static class Course {
101    @PrimaryKey
102    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
103    private Key key;
104
105    private String department;
106
107    public Key getKey() {
108      return key;
109    }
110
111    public String getDepartment() {
112      return department;
113    }
114
115    public void setDepartment(String department) {
116      this.department = department;
117    }
118
119    @Override
120    public boolean equals(Object o) {
121      if (this == o) {
122        return true;
123      }
124      if (o == null || getClass() != o.getClass()) {
125        return false;
126      }
127
128      Course course = (Course) o;
129
130      if (key != null ? !key.equals(course.key) : course.key != null) {
131        return false;
132      }
133
134      return true;
135    }
136
137    @Override
138    public int hashCode() {
139      return key != null ? key.hashCode() : 0;
140    }
141  }
142
143  @PersistenceCapable(detachable = "true")
144  public static class Major {
145    @PrimaryKey
146    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
147    private Key key;
148
149    private String school;
150
151    public Key getKey() {
152      return key;
153    }
154
155    public void setKey(Key key) {
156      this.key = key;
157    }
158
159    public String getSchool() {
160      return school;
161    }
162
163    public void setSchool(String school) {
164      this.school = school;
165    }
166
167    @Override
168    public boolean equals(Object o) {
169      if (this == o) {
170        return true;
171      }
172      if (o == null || getClass() != o.getClass()) {
173        return false;
174      }
175
176      Major major = (Major) o;
177
178      if (key != null ? !key.equals(major.key) : major.key != null) {
179        return false;
180      }
181
182      return true;
183    }
184
185    @Override
186    public int hashCode() {
187      int result = key != null ? key.hashCode() : 0;
188      result = 31 * result + (school != null ? school.hashCode() : 0);
189      return result;
190    }
191  }
192
193  public static Student newStudent(int grade, Course... courses) {
194    return newStudent(grade, null, courses);
195  }
196
197  public static Student newStudent(int grade, Major major, Course... courses) {
198    Student s = new Student();
199    s.setGrade(grade);
200    List<Key> courseKeys = Utils.newArrayList();
201    for (Course c : courses) {
202      courseKeys.add(c.getKey());
203    }
204    s.setCourses(Arrays.asList(courses));
205    if (major != null) {
206      s.setMajor(major);
207    }
208    return s;
209  }
210
211  public static Course newCourse(String dept) {
212    Course c = new Course();
213    c.setDepartment(dept);
214    return c;
215  }
216
217  public static Major newMajor(String school) {
218    Major m = new Major();
219    m.setSchool(school);
220    return m;
221  }
222}