KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > whereused > PersistenceXmlWhereUsedRefactoring


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring.whereused;
21
22 import java.text.MessageFormat JavaDoc;
23 import javax.jmi.reflect.RefObject;
24 import org.netbeans.jmi.javamodel.JavaClass;
25 import org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.PersistenceUnit;
26 import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
27 import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
28 import org.netbeans.modules.j2ee.refactoring.PersistenceRefactoringUtil;
29 import org.netbeans.modules.j2ee.refactoring.Utility;
30 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
31 import org.netbeans.modules.refactoring.api.Problem;
32 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
33 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
34 import org.openide.filesystems.FileObject;
35 import org.openide.util.NbBundle;
36
37 /**
38  * This class collects usages of managed classes listed
39  * in the persistence.xml file.
40  *
41  * @author Erno Mononen
42  */

43 public class PersistenceXmlWhereUsedRefactoring {
44     
45     /**
46      * Creates a new instance of PersistenceXmlWhereUsedRefactoring
47      */

48     public PersistenceXmlWhereUsedRefactoring() {
49     }
50     
51     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, RefactoringElementsBag refactoringElements) {
52         
53         Problem problem = null;
54         
55         if (!(refObject instanceof JavaClass)) {
56             return null;
57         }
58         
59         JavaClass javaClass = (JavaClass)refObject;
60         
61         for (FileObject ddFile : PersistenceRefactoringUtil.getPersistence(javaClass)) {
62             
63             PUDataObject dataObject = ProviderUtil.getPUDataObject(ddFile);
64             
65             if (!ProviderUtil.isValid(dataObject)){
66                 Problem newProblem =
67                         new Problem(false, NbBundle.getMessage(PersistenceXmlWhereUsedRefactoring.class, "TXT_PersistenceXmlInvalidProblem", ddFile.getPath()));
68                 problem = Utility.addProblemsToEnd(problem, newProblem);
69                 continue;
70             }
71             
72             PersistenceUnit[] persistenceUnits = ProviderUtil.getPersistenceUnits(dataObject);
73             
74             for (int i = 0; i < persistenceUnits.length; i++) {
75                 PersistenceUnit unit = persistenceUnits[i];
76                 for (int j = 0; j < unit.getClass2().length; j++) {
77                     if (unit.getClass2()[j].equals(javaClass.getName())){
78                         RefactoringElementImplementation refactoringElem =
79                                 new PersistenceXmlWhereUsedElement(javaClass.getName(), ddFile);
80                         refactoringElements.add(refactoring, refactoringElem);
81                     }
82                     
83                 }
84             }
85         }
86         
87         return problem;
88         
89     }
90     
91     private static class PersistenceXmlWhereUsedElement extends AbstractWhereUsedRefactoringElement {
92         
93         public PersistenceXmlWhereUsedElement(String JavaDoc name, FileObject parentFile) {
94             this.name = name;
95             this.parentFile = parentFile;
96         }
97         
98         public String JavaDoc getDisplayText() {
99             Object JavaDoc[] args = new Object JavaDoc [] {name};
100             return MessageFormat.format(NbBundle.getMessage(PersistenceXmlWhereUsedRefactoring.class, "TXT_PersistenceXmlClassWhereUsed"), args);
101         }
102     }
103     
104 }
105
Popular Tags