Skip to main content
summaryrefslogtreecommitdiffstats
blob: 789167e137527219bf539db8c963241be70f05f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jpt.core.internal.content.java.JavaPersistentType;
import org.eclipse.jpt.core.internal.content.java.JpaCompilationUnit;
import org.eclipse.jpt.core.internal.facet.JpaFacetUtils;
import org.eclipse.jpt.core.internal.platform.IContext;
import org.eclipse.jpt.db.internal.ConnectionProfile;
import org.eclipse.jpt.db.internal.JptDbPlugin;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.CommandExecutor;
import org.eclipse.jpt.utility.internal.CommandExecutorProvider;
import org.eclipse.jpt.utility.internal.iterators.CloneIterator;
import org.eclipse.jst.j2ee.internal.J2EEConstants;
import org.eclipse.wst.common.componentcore.internal.util.IModuleConstants;
import org.eclipse.wst.common.project.facet.core.FacetedProjectFramework;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

/**
 * <!-- begin-user-doc -->
 * A representation of the model object '<em><b>Jpa Project</b></em>'.
 * <!-- end-user-doc -->
 *
 * <p>
 * The following features are supported:
 * <ul>
 *   <li>{@link org.eclipse.jpt.core.internal.JpaProject#getPlatform <em>Platform</em>}</li>
 *   <li>{@link org.eclipse.jpt.core.internal.JpaProject#getDataSource <em>Data Source</em>}</li>
 *   <li>{@link org.eclipse.jpt.core.internal.JpaProject#isDiscoverAnnotatedClasses <em>Discover Annotated Classes</em>}</li>
 *   <li>{@link org.eclipse.jpt.core.internal.JpaProject#getFiles <em>Files</em>}</li>
 * </ul>
 * </p>
 *
 * @see org.eclipse.jpt.core.internal.JpaCorePackage#getJpaProject()
 * @model kind="class"
 * @generated
 */
public class JpaProject extends JpaEObject implements IJpaProject
{
	/**
	 * The IProject associated with this JPA project
	 */
	protected IProject project;

	/**
	 * The IJpaPlatform associated with this JPA project
	 */
	protected IJpaPlatform platform;

	/**
	 * The cached value of the '{@link #getDataSource() <em>Data Source</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataSource()
	 * @generated
	 * @ordered
	 */
	protected IJpaDataSource dataSource;

	/**
	 * The default value of the '{@link #isDiscoverAnnotatedClasses() <em>Discover Annotated Classes</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isDiscoverAnnotatedClasses()
	 * @generated
	 * @ordered
	 */
	protected static final boolean DISCOVER_ANNOTATED_CLASSES_EDEFAULT = false;

	/**
	 * The cached value of the '{@link #isDiscoverAnnotatedClasses() <em>Discover Annotated Classes</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isDiscoverAnnotatedClasses()
	 * @generated
	 * @ordered
	 */
	protected boolean discoverAnnotatedClasses = DISCOVER_ANNOTATED_CLASSES_EDEFAULT;

	/**
	 * This is true if the Discover Annotated Classes attribute has been set.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 * @ordered
	 */
	protected boolean discoverAnnotatedClassesESet;

	/**
	 * The cached value of the '{@link #getFiles() <em>Files</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFiles()
	 * @generated
	 * @ordered
	 */
	protected EList<IJpaFile> files;

	/**
	 * Flag to indicate whether this project has been filled with files
	 */
	private boolean filled = false;

	/**
	 * Flag to indicate that the resynchJob has been scheduled or is running.
	 * This is set to false when that job is completed 
	 */
	boolean resynching = false;
	
	/**
	 * Flag to indicate that the disposing job has been scheduled or is running
	 * (or has been run, in some cases)
	 */
	boolean disposing = false;

	/**
	 * Flag to indicate that the resynchJob needs to be run.  This is
	 * set to true if resynching = true so that the next time the job completes
	 * it will be run again. 
	 */
	boolean needsToResynch = false;

	/**
	 * Job used to reconnect the model parts throughout the project containment hierarchy
	 * @see IJpaProject#resynch()
	 */
	Job resynchJob;

	private IJobChangeListener resynchJobListener;

	private CommandExecutorProvider modifySharedDocumentCommandExecutorProvider;

	private ThreadLocal<CommandExecutor> threadLocalModifySharedDocumentCommandExecutor = new ThreadLocal<CommandExecutor>();


	JpaProject() {
		super();
	}

	protected JpaProject(IProject project) {
		this();
		this.project = project;
		this.resynchJob = buildResynchJob();
		this.resynchJobListener = buildResynchJobListener();
		Job.getJobManager().addJobChangeListener(this.resynchJobListener);
	}

	private Job buildResynchJob() {
		Job job = new Job("Resynching JPA model ...") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				return runResynch(monitor);
			}

		};
		job.setRule(this.project);
		return job;
	}

	private IStatus runResynch(IProgressMonitor monitor) {
		IContext contextHierarchy = getPlatform().buildProjectContext();
		if (monitor.isCanceled()) {
			return Status.CANCEL_STATUS;
		}
		try {
			getPlatform().resynch(contextHierarchy, monitor);
		}
		catch (OperationCanceledException e) {
			return Status.CANCEL_STATUS;
		}
		catch (Throwable e) {
			//exceptions can occur when this thread is running and changes are
			//made to the java source.  our model is not yet updated to the changed java source.
			//log these exceptions and assume they won't happen when the resynch runs again
			//as a result of the java source changes.
			JptCorePlugin.log(e);
		}
		return Status.OK_STATUS;
	}
	
	private IJobChangeListener buildResynchJobListener() {
		return new JobChangeAdapter() {
			@Override
			public void done(IJobChangeEvent event) {
				super.done(event);
				if (event.getJob() == JpaProject.this.resynchJob) {
					resynching = false;
					if (needsToResynch) {
						resynch();
					}
				}
			}
		};
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return JpaCorePackage.Literals.JPA_PROJECT;
	}

	/**
	 * @see IJpaProject#getProject()
	 */
	public IProject getProject() {
		return project;
	}

	public IJavaProject getJavaProject() {
		return JavaCore.create(getProject());
	}

	/**
	 * @see IJpaProject#getModel()
	 */
	public IJpaModel getModel() {
		return (IJpaModel) eContainer();
	}

	/**
	 * Returns the value of the '<em><b>Platform</b></em>' reference.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Platform</em>' reference isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Platform</em>' reference.
	 * @see #setPlatform(IJpaPlatform)
	 * @see org.eclipse.jpt.core.internal.JpaCorePackage#getJpaProject_Platform()
	 * @model resolveProxies="false" required="true" ordered="false"
	 * @generated
	 */
	public IJpaPlatform getPlatformGen() {
		return platform;
	}

	public IJpaPlatform getPlatform() {
		if (platform == null) {
			setPlatform(JpaFacetUtils.getPlatform(project));
		}
		return getPlatformGen();
	}

	/**
	 * Sets the value of the '{@link org.eclipse.jpt.core.internal.JpaProject#getPlatform <em>Platform</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Platform</em>' reference.
	 * @see #getPlatform()
	 * @generated
	 */
	public void setPlatformGen(IJpaPlatform newPlatform) {
		IJpaPlatform oldPlatform = platform;
		platform = newPlatform;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, JpaCorePackage.JPA_PROJECT__PLATFORM, oldPlatform, platform));
	}

	public void setPlatform(IJpaPlatform jpaPlatform) {
		jpaPlatform.setProject(this);
		setPlatformGen(jpaPlatform);
	}

	/**
	 * @see IJpaProject#setPlatform(String)
	 */
	public void setPlatform(String platformId) {
		setPlatform(JpaPlatformRegistry.instance().jpaPlatform(platformId));
	}

	/**
	 * Returns the value of the '<em><b>Data Source</b></em>' containment reference.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Data Source</em>' containment reference isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Data Source</em>' containment reference.
	 * @see #setDataSource(IJpaDataSource)
	 * @see org.eclipse.jpt.core.internal.JpaCorePackage#getJpaProject_DataSource()
	 * @model containment="true" ordered="false"
	 * @generated
	 */
	public IJpaDataSource getDataSourceGen() {
		return dataSource;
	}

	public synchronized IJpaDataSource getDataSource() {
		if (dataSource == null) {
			setDataSource(JpaFacetUtils.getConnectionName(project));
		}
		return getDataSourceGen();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetDataSource(IJpaDataSource newDataSource, NotificationChain msgs) {
		IJpaDataSource oldDataSource = dataSource;
		dataSource = newDataSource;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, JpaCorePackage.JPA_PROJECT__DATA_SOURCE, oldDataSource, newDataSource);
			if (msgs == null)
				msgs = notification;
			else
				msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.jpt.core.internal.JpaProject#getDataSource <em>Data Source</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Data Source</em>' containment reference.
	 * @see #getDataSource()
	 * @generated
	 */
	public void setDataSource(IJpaDataSource newDataSource) {
		if (newDataSource != dataSource) {
			NotificationChain msgs = null;
			if (dataSource != null)
				msgs = ((InternalEObject) dataSource).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - JpaCorePackage.JPA_PROJECT__DATA_SOURCE, null, msgs);
			if (newDataSource != null)
				msgs = ((InternalEObject) newDataSource).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - JpaCorePackage.JPA_PROJECT__DATA_SOURCE, null, msgs);
			msgs = basicSetDataSource(newDataSource, msgs);
			if (msgs != null)
				msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, JpaCorePackage.JPA_PROJECT__DATA_SOURCE, newDataSource, newDataSource));
	}

	/**
	 * @see IJpaProject#setDataSource(String)
	 */
	public void setDataSource(String connectionProfileName) {
		if (dataSource == null) {
			JpaDataSource ds = JpaCoreFactory.eINSTANCE.createJpaDataSource();
			setDataSource(ds);
		}
		dataSource.setConnectionProfileName(connectionProfileName);
	}

	/**
	 * Returns the value of the '<em><b>Discover Annotated Classes</b></em>' attribute.
	 * The default value is <code>"false"</code>.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Discover Annotated Classes</em>' attribute isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Discover Annotated Classes</em>' attribute.
	 * @see #isSetDiscoverAnnotatedClasses()
	 * @see #unsetDiscoverAnnotatedClasses()
	 * @see #setDiscoverAnnotatedClasses(boolean)
	 * @see org.eclipse.jpt.core.internal.JpaCorePackage#getJpaProject_DiscoverAnnotatedClasses()
	 * @model default="false" unique="false" unsettable="true" required="true" ordered="false"
	 * @generated
	 */
	public boolean isDiscoverAnnotatedClassesGen() {
		return discoverAnnotatedClasses;
	}

	public boolean isDiscoverAnnotatedClasses() {
		if (!isSetDiscoverAnnotatedClasses()) {
			setDiscoverAnnotatedClasses(JpaFacetUtils.getDiscoverAnnotatedClasses(project));
		}
		return isDiscoverAnnotatedClassesGen();
	}

	/**
	 * Sets the value of the '{@link org.eclipse.jpt.core.internal.JpaProject#isDiscoverAnnotatedClasses <em>Discover Annotated Classes</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Discover Annotated Classes</em>' attribute.
	 * @see #isSetDiscoverAnnotatedClasses()
	 * @see #unsetDiscoverAnnotatedClasses()
	 * @see #isDiscoverAnnotatedClasses()
	 * @generated
	 */
	public void setDiscoverAnnotatedClasses(boolean newDiscoverAnnotatedClasses) {
		boolean oldDiscoverAnnotatedClasses = discoverAnnotatedClasses;
		discoverAnnotatedClasses = newDiscoverAnnotatedClasses;
		boolean oldDiscoverAnnotatedClassesESet = discoverAnnotatedClassesESet;
		discoverAnnotatedClassesESet = true;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES, oldDiscoverAnnotatedClasses, discoverAnnotatedClasses, !oldDiscoverAnnotatedClassesESet));
	}

	/**
	 * Unsets the value of the '{@link org.eclipse.jpt.core.internal.JpaProject#isDiscoverAnnotatedClasses <em>Discover Annotated Classes</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isSetDiscoverAnnotatedClasses()
	 * @see #isDiscoverAnnotatedClasses()
	 * @see #setDiscoverAnnotatedClasses(boolean)
	 * @generated
	 */
	public void unsetDiscoverAnnotatedClasses() {
		boolean oldDiscoverAnnotatedClasses = discoverAnnotatedClasses;
		boolean oldDiscoverAnnotatedClassesESet = discoverAnnotatedClassesESet;
		discoverAnnotatedClasses = DISCOVER_ANNOTATED_CLASSES_EDEFAULT;
		discoverAnnotatedClassesESet = false;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.UNSET, JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES, oldDiscoverAnnotatedClasses, DISCOVER_ANNOTATED_CLASSES_EDEFAULT, oldDiscoverAnnotatedClassesESet));
	}

	/**
	 * Returns whether the value of the '{@link org.eclipse.jpt.core.internal.JpaProject#isDiscoverAnnotatedClasses <em>Discover Annotated Classes</em>}' attribute is set.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return whether the value of the '<em>Discover Annotated Classes</em>' attribute is set.
	 * @see #unsetDiscoverAnnotatedClasses()
	 * @see #isDiscoverAnnotatedClasses()
	 * @see #setDiscoverAnnotatedClasses(boolean)
	 * @generated
	 */
	public boolean isSetDiscoverAnnotatedClasses() {
		return discoverAnnotatedClassesESet;
	}

	public String rootDeployLocation() {
		String metaInfLocation = "";
		try {
			if (FacetedProjectFramework.hasProjectFacet(project, IModuleConstants.JST_WEB_MODULE)) {
				metaInfLocation = J2EEConstants.WEB_INF_CLASSES;
			}
		}
		catch (CoreException ce) {
			// if exception occurs, we'll take the default location
			JptCorePlugin.log(ce);
		}
		return metaInfLocation;
	}

	@Override
	public IJpaProject getJpaProject() {
		return this;
	}

	/**
	 * Returns the value of the '<em><b>Files</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.jpt.core.internal.IJpaFile}.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Files</em>' containment reference list isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Files</em>' containment reference list.
	 * @see org.eclipse.jpt.core.internal.JpaCorePackage#getJpaProject_Files()
	 * @model containment="true"
	 * @generated
	 */
	public EList<IJpaFile> getFiles() {
		if (files == null) {
			files = new EObjectContainmentEList<IJpaFile>(IJpaFile.class, this, JpaCorePackage.JPA_PROJECT__FILES);
		}
		return files;
	}

	/**
	 * INTERNAL ONLY
	 * Fill project with file resources
	 */
	void fill() throws CoreException {
		if (filled) {
			return;
		}
		IResourceProxyVisitor visitor = new IResourceProxyVisitor() {
			public boolean visit(IResourceProxy resource) throws CoreException {
				switch (resource.getType()) {
					case IResource.PROJECT :
					case IResource.FOLDER :
						return true;
					case IResource.FILE :
						IFile file = (IFile) resource.requestResource();
						if (getJpaFileInternal(file) == null) {
							createJpaFile(file);
						}
					default :
						return false;
				}
			}
		};
		getProject().accept(visitor, IResource.NONE);
		filled = true;
		resynch();
	}

	public boolean isFilled() {
		return filled;
	}

	/**
	 * @see IJpaProject#getJpaFile(IFile)
	 */
	public synchronized IJpaFile getJpaFile(IFile file) {
		IJpaFile jpaFile = this.getJpaFileInternal(file);
		if (this.filled) {
			return jpaFile;
		}
		return (jpaFile != null) ? jpaFile : this.createJpaFile(file);
	}

	synchronized IJpaFile getJpaFileInternal(IFile file) {
		for (IJpaFile next : this.getFiles()) {
			if (next.getFile().equals(file)) {
				return next;
			}
		}
		return null;
	}

	public Iterator<IJpaFile> jpaFiles() {
		return new CloneIterator<IJpaFile>(getFiles());
	}

	public Collection<IJpaFile> jpaFiles(String contentType) {
		Collection<IJpaFile> jpaFiles = new ArrayList<IJpaFile>();
		for (Iterator<IJpaFile> stream = jpaFiles(); stream.hasNext();) {
			IJpaFile next = stream.next();
			if (next.getContentId().equals(contentType)) {
				jpaFiles.add(next);
			}
		}
		return jpaFiles;
	}

	public JavaPersistentType findJavaPersistentType(IType type) {
		if (type == null) {
			return null;
		}
		Collection<IJpaFile> persistenceFiles = jpaFiles(JptCorePlugin.JAVA_CONTENT_TYPE);
		for (IJpaFile jpaFile : persistenceFiles) {
			JpaCompilationUnit compilationUnit = (JpaCompilationUnit) jpaFile.getContent();
			for (JavaPersistentType persistentType : compilationUnit.getTypes()) {
				if (type.equals(persistentType.getType().getJdtMember())) {
					return persistentType;
				}
			}
		}
		return null;
	}

	/**
	 * INTERNAL ONLY
	 * Dispose and remove project
	 */
	void dispose() {
		if (disposing) return;
		
		disposing = true;
				
		Job job = new Job("Disposing JPA project ...") {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				dispose_();
				return Status.OK_STATUS;
			}
		};
		job.setRule(project);
		job.schedule();
	}
	
	private void dispose_() {
		Job.getJobManager().removeJobChangeListener(resynchJobListener);
		for (IJpaFile jpaFile : CollectionTools.collection(getFiles())) {
			((JpaFile) jpaFile).dispose();
		}
		((JpaModel) getModel()).getProjects().remove(this);
	}

	/**
	 * INTERNAL ONLY
	 * Synch internal JPA resources.
	 */
	void synchInternalResources(IResourceDelta delta) throws CoreException {
		delta.accept(this.buildResourceDeltaVisitor());
	}

	private IResourceDeltaVisitor buildResourceDeltaVisitor() {
		return new IResourceDeltaVisitor() {
			private IResource currentResource = getProject();

			public boolean visit(IResourceDelta delta) throws CoreException {
				IResource res = delta.getResource();
				if (res.equals(currentResource))
					return true;
				if (res.getType() == IResource.FILE) {
					IFile file = (IFile) res;
					switch (delta.getKind()) {
						case IResourceDelta.ADDED :
							if (getJpaFile(file) == null) {
								createJpaFile(file);
								JpaProject.this.resynch();//TODO different api for this?
							}
							break;
						case IResourceDelta.REMOVED :
							JpaFile jpaFile = (JpaFile) getJpaFile(file);
							if (jpaFile != null) {
								jpaFile.dispose();
								JpaProject.this.resynch();//TODO different api for this?
							}
							break;
						case IResourceDelta.CHANGED :
							// shouldn't have to worry, since all changes we're looking for have to do with file name
					}
				}
				return true;
			}
		};
	}

		// PWFTODO 
		// Return a NullPersistenceFile if no content found?
	synchronized IJpaFile createJpaFile(IFile file) {
		if (!JavaCore.create(this.project).isOnClasspath(file)) {
			return null;
		}

		IContentType contentType = this.contentType(file);
		if (contentType == null) {
			return null;
		}

		String contentTypeId = contentType.getId();
		IJpaFileContentProvider provider = this.getPlatform().fileContentProvider(contentTypeId);
		if (provider == null) {
			return null;
		}

		JpaFile jpaFile = JpaCoreFactory.eINSTANCE.createJpaFile();
		this.getFiles().add(jpaFile);
		jpaFile.setFile(file);
		jpaFile.setContentId(contentTypeId);
		provider.buildRootContent(jpaFile);
		return jpaFile;
	}

	//attempting to get the contentType based on the file contents.
	//have to check the file contents instead of just the file name
	//because for xml we base it on the rootElement name
	private IContentType contentType(IFile file) {
		try {
			return Platform.getContentTypeManager().findContentTypeFor(file.getContents(), file.getName());
		} catch (IOException ex) {
			JptCorePlugin.log(ex);
		} catch (CoreException ex) {
			JptCorePlugin.log(ex);
		}
		return null;
	}

	/**
	 * INTERNAL ONLY
	 * Handle java element change event.
	 */
	void handleEvent(ElementChangedEvent event) {
		if (filled) {
			for (Iterator<IJpaFile> stream = jpaFiles(); stream.hasNext();) {
				((JpaFile) stream.next()).handleEvent(event);
			}
			resynch();
		}
	}

	public Iterator<IMessage> validationMessages() {
		List<IMessage> messages = new ArrayList<IMessage>();
		getPlatform().addToMessages(messages);
		return messages.iterator();
	}

	//leaving this at the JpaProject level for now instead of
	//passing it on to the JpaModel.  We don't currently support
	//multiple projects having cross-references
	public void resynch() {
		if (disposing || ! filled) return;
		
		if (! this.resynching) {
			this.resynching = true;
			this.needsToResynch = false;
			this.resynchJob.schedule();
		}
		else {
			this.needsToResynch = true;
			this.resynchJob.cancel();
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case JpaCorePackage.JPA_PROJECT__DATA_SOURCE :
				return basicSetDataSource(null, msgs);
			case JpaCorePackage.JPA_PROJECT__FILES :
				return ((InternalEList<?>) getFiles()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case JpaCorePackage.JPA_PROJECT__PLATFORM :
				return getPlatform();
			case JpaCorePackage.JPA_PROJECT__DATA_SOURCE :
				return getDataSource();
			case JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES :
				return isDiscoverAnnotatedClasses() ? Boolean.TRUE : Boolean.FALSE;
			case JpaCorePackage.JPA_PROJECT__FILES :
				return getFiles();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case JpaCorePackage.JPA_PROJECT__PLATFORM :
				setPlatform((IJpaPlatform) newValue);
				return;
			case JpaCorePackage.JPA_PROJECT__DATA_SOURCE :
				setDataSource((IJpaDataSource) newValue);
				return;
			case JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES :
				setDiscoverAnnotatedClasses(((Boolean) newValue).booleanValue());
				return;
			case JpaCorePackage.JPA_PROJECT__FILES :
				getFiles().clear();
				getFiles().addAll((Collection<? extends IJpaFile>) newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case JpaCorePackage.JPA_PROJECT__PLATFORM :
				setPlatform((IJpaPlatform) null);
				return;
			case JpaCorePackage.JPA_PROJECT__DATA_SOURCE :
				setDataSource((IJpaDataSource) null);
				return;
			case JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES :
				unsetDiscoverAnnotatedClasses();
				return;
			case JpaCorePackage.JPA_PROJECT__FILES :
				getFiles().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case JpaCorePackage.JPA_PROJECT__PLATFORM :
				return platform != null;
			case JpaCorePackage.JPA_PROJECT__DATA_SOURCE :
				return dataSource != null;
			case JpaCorePackage.JPA_PROJECT__DISCOVER_ANNOTATED_CLASSES :
				return isSetDiscoverAnnotatedClasses();
			case JpaCorePackage.JPA_PROJECT__FILES :
				return files != null && !files.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	@Override
	public String toString() {
		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (" + getProject().toString() + ")");
		return result.toString();
	}

	@Override
	public ConnectionProfile connectionProfile() {
		String profileName = getDataSource().getConnectionProfileName();
		return JptDbPlugin.getDefault().getConnectionProfileRepository().profileNamed(profileName);
	}

	public CommandExecutor getThreadLocalModifySharedDocumentCommandExecutor() {
		CommandExecutor commandExecutor = this.threadLocalModifySharedDocumentCommandExecutor.get();
		return (commandExecutor != null) ? commandExecutor : CommandExecutor.Default.instance();
	}

	public void setThreadLocalModifySharedDocumentCommandExecutor(CommandExecutor commandExecutor) {
		this.threadLocalModifySharedDocumentCommandExecutor.set(commandExecutor);
	}

	public CommandExecutorProvider modifySharedDocumentCommandExecutorProvider() {
		if (this.modifySharedDocumentCommandExecutorProvider == null) {
			this.modifySharedDocumentCommandExecutorProvider = new ModifySharedDocumentCommandExecutorProvider();
		}
		return this.modifySharedDocumentCommandExecutorProvider;
	}
	
	
	// ********** member class **********
	
	private class ModifySharedDocumentCommandExecutorProvider 
			implements CommandExecutorProvider 
	{
		ModifySharedDocumentCommandExecutorProvider() {
			super();
		}
		
		public CommandExecutor commandExecutor() {
			return JpaProject.this.getThreadLocalModifySharedDocumentCommandExecutor();
		}
	}
}

Back to the top