blob: bdd4cd8ed688a18b314c7c4e163c93aed2c04bf8 [file] [log] [blame]
kmoorec9c9e2b2011-02-06 02:07:28 +00001/*******************************************************************************
bvosburgh91d39dc2011-04-28 02:00:43 +00002 * Copyright (c) 2009, 2011 Oracle. All rights reserved.
kmoorec9c9e2b2011-02-06 02:07:28 +00003 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0, which accompanies this distribution
5 * and is available at http://www.eclipse.org/legal/epl-v10.html.
6 *
7 * Contributors:
8 * Oracle - initial API and implementation
9 ******************************************************************************/
10package org.eclipse.jpt.jpa.core.internal.jpa2.context.java;
11
12import java.util.List;
13import org.eclipse.jdt.core.dom.CompilationUnit;
kmoorec7a08672011-08-01 15:10:18 +000014import org.eclipse.jpt.common.core.resource.java.JavaResourceAttribute;
kmoorec9c9e2b2011-02-06 02:07:28 +000015import org.eclipse.jpt.common.core.utility.TextRange;
16import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaJpaContextNode;
17import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaDerivedIdentity2_0;
18import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaIdDerivedIdentityStrategy2_0;
19import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaSingleRelationshipMapping2_0;
20import org.eclipse.jpt.jpa.core.resource.java.IdAnnotation;
kmoorec9c9e2b2011-02-06 02:07:28 +000021import org.eclipse.wst.validation.internal.provisional.core.IMessage;
22import org.eclipse.wst.validation.internal.provisional.core.IReporter;
23
24public class GenericJavaIdDerivedIdentityStrategy2_0
25 extends AbstractJavaJpaContextNode
26 implements JavaIdDerivedIdentityStrategy2_0
27{
28 protected boolean value;
29
30
31 public GenericJavaIdDerivedIdentityStrategy2_0(JavaDerivedIdentity2_0 parent) {
32 super(parent);
33 this.value = this.buildValue();
34 }
35
36
37 // ********** synchronize/update **********
38
39 @Override
40 public void synchronizeWithResourceModel() {
41 super.synchronizeWithResourceModel();
42 this.setValue_(this.buildValue());
43 }
44
45
46 // ********** value **********
47
48 public boolean getValue() {
49 return this.value;
50 }
51
52 public void setValue(boolean value) {
53 if (value != this.value) {
54 if (value) {
55 this.addAnnotation();
56 } else {
57 this.removeAnnotation();
58 }
59 this.setValue_(value);
60 }
61 }
62
63 protected void setValue_(boolean value) {
64 boolean old = this.value;
65 this.value = value;
66 this.firePropertyChanged(VALUE_PROPERTY, old, value);
67 }
68
69 protected boolean buildValue() {
70 return this.getAnnotation() != null;
71 }
72
73
74 // ********** annotation **********
75
76 protected IdAnnotation getAnnotation() {
77 return (IdAnnotation) this.getResourceAttribute().getAnnotation(this.getAnnotationName());
78 }
79
80 protected void addAnnotation() {
81 this.getResourceAttribute().addAnnotation(this.getAnnotationName());
82 }
83
84 protected void removeAnnotation() {
85 this.getResourceAttribute().removeAnnotation(this.getAnnotationName());
86 }
87
88 protected String getAnnotationName() {
89 return IdAnnotation.ANNOTATION_NAME;
90 }
91
92
93 // ********** misc **********
94
95 @Override
96 public JavaDerivedIdentity2_0 getParent() {
97 return (JavaDerivedIdentity2_0) super.getParent();
98 }
99
100 protected JavaDerivedIdentity2_0 getDerivedIdentity() {
101 return this.getParent();
102 }
103
104 protected JavaSingleRelationshipMapping2_0 getMapping() {
105 return this.getDerivedIdentity().getMapping();
106 }
107
kmoorec7a08672011-08-01 15:10:18 +0000108 protected JavaResourceAttribute getResourceAttribute() {
109 return this.getMapping().getPersistentAttribute().getResourceAttribute();
kmoorec9c9e2b2011-02-06 02:07:28 +0000110 }
111
112 public boolean isSpecified() {
113 return this.value;
114 }
115
116 public void addStrategy() {
117 this.setValue(true);
118 }
119
120 public void removeStrategy() {
121 this.setValue(false);
122 }
123
124
125 // ********** validation **********
126
127 @Override
128 public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
129 super.validate(messages, reporter, astRoot);
130 // no validation rules
131 }
132
133 public TextRange getValidationTextRange(CompilationUnit astRoot) {
bvosburgh91d39dc2011-04-28 02:00:43 +0000134 TextRange textRange = this.getAnnotationTextRange(astRoot);
135 return (textRange != null) ? textRange : this.getDerivedIdentity().getValidationTextRange(astRoot);
136 }
137
138 protected TextRange getAnnotationTextRange(CompilationUnit astRoot) {
kmoorec9c9e2b2011-02-06 02:07:28 +0000139 IdAnnotation annotation = this.getAnnotation();
140 return (annotation == null) ? null : annotation.getTextRange(astRoot);
141 }
142}