PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 221 lines | 158 code | 45 blank | 18 comment | 34 complexity | 85e943045804a98d53506f59215c1981 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.List;
 24
 25import javax.jdo.annotations.Element;
 26import javax.jdo.annotations.IdGeneratorStrategy;
 27import javax.jdo.annotations.PersistenceCapable;
 28import javax.jdo.annotations.Persistent;
 29import javax.jdo.annotations.PrimaryKey;
 30
 31/**
 32 * @author Max Ross <maxr@google.com>
 33 */
 34public class UnownedJoinsJDO {
 35
 36  @PersistenceCapable(detachable = "true")
 37  public static class Student {
 38    @PrimaryKey
 39    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 40    private Long id;
 41
 42    private int grade;
 43
 44    @Element(dependent = "true")
 45    private List<Key> courses = new ArrayList<Key>();
 46
 47    @Persistent
 48    private Key major;
 49
 50    public int getGrade() {
 51      return grade;
 52    }
 53
 54    public void setGrade(int grade) {
 55      this.grade = grade;
 56    }
 57
 58    public List<Key> getCourses() {
 59      return courses;
 60    }
 61
 62    public void setCourses(List<Key> courses) {
 63      this.courses = courses;
 64    }
 65
 66    public Key getMajor() {
 67      return major;
 68    }
 69
 70    public void setMajor(Key major) {
 71      this.major = major;
 72    }
 73
 74    @Override
 75    public boolean equals(Object o) {
 76      if (this == o) {
 77        return true;
 78      }
 79      if (o == null || getClass() != o.getClass()) {
 80        return false;
 81      }
 82
 83      Student student = (Student) o;
 84
 85      if (id != null ? !id.equals(student.id) : student.id != null) {
 86        return false;
 87      }
 88
 89      return true;
 90    }
 91
 92    @Override
 93    public int hashCode() {
 94      return id != null ? id.hashCode() : 0;
 95    }
 96  }
 97  
 98  @PersistenceCapable(detachable = "true")
 99  public static class Course {
100    @PrimaryKey
101    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
102    private Key key;
103
104    private String department;
105
106    public Key getKey() {
107      return key;
108    }
109
110    public String getDepartment() {
111      return department;
112    }
113
114    public void setDepartment(String department) {
115      this.department = department;
116    }
117
118    @Override
119    public boolean equals(Object o) {
120      if (this == o) {
121        return true;
122      }
123      if (o == null || getClass() != o.getClass()) {
124        return false;
125      }
126
127      Course course = (Course) o;
128
129      if (key != null ? !key.equals(course.key) : course.key != null) {
130        return false;
131      }
132
133      return true;
134    }
135
136    @Override
137    public int hashCode() {
138      return key != null ? key.hashCode() : 0;
139    }
140  }
141
142  @PersistenceCapable(detachable = "true")
143  public static class Major {
144    @PrimaryKey
145    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
146    private Key key;
147
148    private String school;
149
150    public Key getKey() {
151      return key;
152    }
153
154    public void setKey(Key key) {
155      this.key = key;
156    }
157
158    public String getSchool() {
159      return school;
160    }
161
162    public void setSchool(String school) {
163      this.school = school;
164    }
165
166    @Override
167    public boolean equals(Object o) {
168      if (this == o) {
169        return true;
170      }
171      if (o == null || getClass() != o.getClass()) {
172        return false;
173      }
174
175      Major major = (Major) o;
176
177      if (key != null ? !key.equals(major.key) : major.key != null) {
178        return false;
179      }
180
181      return true;
182    }
183
184    @Override
185    public int hashCode() {
186      int result = key != null ? key.hashCode() : 0;
187      result = 31 * result + (school != null ? school.hashCode() : 0);
188      return result;
189    }
190  }
191
192  public static Student newStudent(int grade, Course... courses) {
193    return newStudent(grade, null, courses);
194  }
195
196  public static Student newStudent(int grade, Major major, Course... courses) {
197    Student s = new Student();
198    s.setGrade(grade);
199    List<Key> courseKeys = Utils.newArrayList();
200    for (Course c : courses) {
201      courseKeys.add(c.getKey());
202    }
203    s.setCourses(courseKeys);
204    if (major != null) {
205      s.setMajor(major.getKey());
206    }
207    return s;
208  }
209
210  public static Course newCourse(String dept) {
211    Course c = new Course();
212    c.setDepartment(dept);
213    return c;
214  }
215
216  public static Major newMajor(String school) {
217    Major m = new Major();
218    m.setSchool(school);
219    return m;
220  }
221}