001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.visualweb.insync;
042:
043: import java.io.IOException;
044:
045: import org.netbeans.api.project.FileOwnerQuery;
046: import org.netbeans.api.project.Project;
047: import org.openide.ErrorManager;
048: import org.openide.filesystems.FileObject;
049: import org.openide.filesystems.FileUtil;
050: import org.openide.filesystems.Repository;
051: import org.openide.loaders.DataObject;
052: import org.openide.loaders.DataObjectNotFoundException;
053:
054: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectConstants;
055: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
056: import org.netbeans.modules.visualweb.insync.faces.FacesUnit;
057: import org.netbeans.modules.visualweb.insync.models.FacesModelSet;
058: import org.netbeans.modules.visualweb.insync.models.FacesConfigModel;
059: import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
060:
061: /**
062: * I represent an object that optimizes the queries that can be performed by the InSyncServiceProvider.
063: * Since InSyncServiceProvider is stateless and that all information is project and model set related, it
064: * was decided that having an object that can cache this information on a per query request from
065: * InSyncServiceProvider would be more efficient than always computing all interdependent values.
066: *
067: * @author eric
068: *
069: */
070: public class InSyncServiceProviderQuery {
071:
072: protected boolean hasInitializedFileObject;
073: protected FileObject fileObject;
074: protected boolean hasInitializedDataObject;
075: protected DataObject dataObject;
076:
077: protected boolean hasInitializedProject;
078: protected Project project;
079: protected boolean hasInitializedFacesModelSet;
080: protected FacesModelSet facesModelSet;
081: protected Boolean isInProjectJspRoot;
082: protected Boolean isInProjectJavaRoot;
083: protected Boolean isTemplateFileObject;
084:
085: public InSyncServiceProviderQuery(FileObject queriedFileObject,
086: DataObject queriedDataObject) {
087: super ();
088: if (queriedFileObject == null && queriedDataObject == null)
089: throw new RuntimeException("Cannot pass in null for both");
090: this .fileObject = queriedFileObject;
091: hasInitializedFileObject = queriedFileObject != null;
092: this .dataObject = queriedDataObject;
093: hasInitializedDataObject = queriedDataObject != null;
094: }
095:
096: public String getBeanName() {
097: if (isInProjectJspRoot()) {
098: // Compute the bean name by
099: String relativePath = FileUtil.getRelativePath(
100: getProjectJspRoot(), getQueriedFileObject());
101: String result = relativePath.replace('/', '$');
102: result = FacesUnit.fixPossiblyImplicitBeanName(result);
103: return result;
104: } else if (isInProjectJavaRoot()) {
105: String relativePath = FileUtil.getRelativePath(
106: getProjectJavaRoot(), getQueriedFileObject());
107: String javaClassName = relativePath.replace('/', '.');
108: ManagedBean managedBean = getManagedBeanWithBeanClass(javaClassName);
109: if (managedBean != null)
110: return managedBean.getManagedBeanName();
111: return null;
112: } else {
113: // If file is not in either root, then assume we cannot compute bean name
114: return null;
115: }
116: }
117:
118: public String getBeanNameViaJsp() {
119: String result;
120: if (isTemplateFileObject()) {
121: result = "";
122: } else {
123: if (!isInProjectJspRoot())
124: return null;
125: // Compute the bean name by
126: String relativePath = FileUtil.getRelativePath(
127: getProjectJspRoot(), getQueriedFileObject()
128: .getParent());
129: result = relativePath.replace('/', '$');
130: if (result.length() > 0)
131: result += "$";
132: }
133: result += getQueriedFileObject().getName(); // NOI18N
134: result = FacesUnit.fixPossiblyImplicitBeanName(result);
135: return result;
136: }
137:
138: public DataObject getDataObject() {
139: if (hasInitializedDataObject)
140: return dataObject;
141: hasInitializedDataObject = true;
142: try {
143: dataObject = DataObject.find(getFileObject());
144: } catch (DataObjectNotFoundException e) {
145: }
146: return dataObject;
147: }
148:
149: public FacesModelSet getFacesModelSet() {
150: if (hasInitializedFacesModelSet)
151: return facesModelSet;
152: hasInitializedFacesModelSet = true;
153: facesModelSet = FacesModelSet.getInstance(getProject());
154: return facesModelSet;
155: }
156:
157: public FileObject getFileObject() {
158: if (hasInitializedFileObject)
159: return fileObject;
160: hasInitializedFileObject = true;
161: DataObject object = getDataObject();
162: if (object != null)
163: fileObject = object.getPrimaryFile();
164: return fileObject;
165: }
166:
167: /**
168: * This version is model safe, does not depend on models being ok.
169: *
170: * @return
171: */
172: public DataObject getJavaDataObjectEquivalent(String originalName,
173: boolean forceCreate) {
174: FileObject fileEquivalent = getJavaFileObjectEquivalent(
175: originalName, forceCreate);
176: if (fileEquivalent == null)
177: return null;
178: try {
179: DataObject result = DataObject.find(fileEquivalent);
180: return result;
181: } catch (DataObjectNotFoundException e) {
182: return null;
183: }
184: }
185:
186: /**
187: * This version is model safe, does not depend on models being ok.
188: * @param forceCreate TODO
189: *
190: * @return
191: */
192: public FileObject getJavaFileObjectEquivalent(String originalName,
193: boolean forceCreate) {
194: if (getFileObject() == null)
195: return null;
196: if (isInProjectJavaRoot())
197: return getFileObject();
198: // If the file is anywhere outside of java and web, we can't compute this ?
199: if (!isInProjectJspRoot())
200: return null;
201: if (getFileObject().isFolder()) {
202: if (getFileObject() == getProjectJspRoot())
203: return getProjectPageBeanRoot();
204: FileObject file;
205: if (originalName == null) {
206: file = getFileObject();
207: } else {
208: file = getFileObject().getParent();
209: }
210: String relative = FileUtil.getRelativePath(
211: getProjectJspRoot(), file);
212: relative = getProjectPageBeanPackageNameWithSlash()
213: + relative;
214: file = getProjectJavaRoot().getFileObject(relative);
215: if (file == null) {
216: if (!forceCreate)
217: return null;
218: try {
219: file = FileUtil.createFolder(getProjectJavaRoot(),
220: relative);
221: } catch (IOException e) {
222: ErrorManager.getDefault().notify(
223: ErrorManager.INFORMATIONAL, e);
224: }
225: }
226: if (originalName != null) {
227: file = file.getFileObject(originalName);
228: if (file == null) {
229: if (!forceCreate)
230: return null;
231: try {
232: file = FileUtil
233: .createFolder(file, originalName);
234: } catch (IOException e) {
235: ErrorManager.getDefault().notify(
236: ErrorManager.INFORMATIONAL, e);
237: }
238: }
239: }
240: return file;
241: }
242: String name;
243: if (originalName == null)
244: name = getFileObject().getName();
245: else
246: name = originalName;
247: String relative = FileUtil.getRelativePath(getProjectJspRoot(),
248: getFileObject().getParent());
249: relative = getProjectPageBeanPackageNameWithSlash() + relative;
250: FileObject file = getProjectJavaRoot().getFileObject(relative);
251: if (file == null)
252: return null;
253: file = file.getFileObject(name, "java");
254: return file;
255: }
256:
257: public FileObject getJavaFolderForJsp() {
258: return null;
259: }
260:
261: public FileObject getJavaForJsp() {
262: if (!isInProjectJspRoot())
263: return null;
264: // String beanName = getBeanNameViaJsp();
265: throw new RuntimeException("No implemented yet !!!");
266: }
267:
268: public ManagedBean getManagedBeanNamed(String name) {
269: FacesConfigModel model = getFacesModelSet()
270: .getFacesConfigModel();
271: ManagedBean result = model.getManagedBean(name);
272: return result;
273: }
274:
275: /**
276: * Return the single managed bean that has name for its managed-bean-class element.
277: * If there are 0, OR more than 1, return null.
278: *
279: * @param name
280: * @return
281: */
282: public ManagedBean getManagedBeanWithBeanClass(String name) {
283: FacesConfigModel model = getFacesModelSet()
284: .getFacesConfigModel();
285: ManagedBean[] managedBeans = model.getManagedBeans();
286: ManagedBean result = null;
287: for (int i = 0; i < managedBeans.length; i++) {
288: ManagedBean managedBean = managedBeans[i];
289: if (name.equals(managedBean.getManagedBeanClass())) {
290: if (result == null)
291: result = managedBean;
292: else
293: // More than 1
294: return null;
295: }
296: }
297: // 0 or 1
298: return result;
299: }
300:
301: public Project getProject() {
302: if (hasInitializedProject)
303: return project;
304: hasInitializedProject = true;
305: project = FileOwnerQuery.getOwner(getQueriedFileObject());
306: return project;
307: }
308:
309: public String getProjectPageBeanPackageNameWithDot() {
310: String result = JsfProjectUtils.getProjectProperty(
311: getProject(),
312: JsfProjectConstants.PROP_JSF_PAGEBEAN_PACKAGE);
313: if (result.length() > 0)
314: result += ".";
315: return result;
316: }
317:
318: public String getProjectPageBeanPackageNameWithSlash() {
319: String result = getProjectPageBeanPackageNameWithDot();
320: result = result.replace('.', '/');
321: return result;
322: }
323:
324: public FileObject getProjectJavaRoot() {
325: Project project = getProject();
326: if (project == null)
327: return null;
328: FileObject result = JsfProjectUtils.getSourceRoot(project);
329: return result;
330: }
331:
332: public FileObject getProjectJspRoot() {
333: Project project = getProject();
334: if (project == null)
335: return null;
336: FileObject result = JsfProjectUtils.getDocumentRoot(project);
337: return result;
338: }
339:
340: public FileObject getProjectPageBeanRoot() {
341: Project project = getProject();
342: if (project == null)
343: return null;
344: FileObject result = JsfProjectUtils.getPageBeanRoot(project);
345: return result;
346: }
347:
348: public FileObject getQueriedFileObject() {
349: return fileObject;
350: }
351:
352: public boolean isInProjectJavaRoot() {
353: if (isInProjectJavaRoot == null) {
354: FileObject root = getProjectJavaRoot();
355: boolean result;
356: if (root == null) {
357: result = false;
358: } else {
359: result = root == getQueriedFileObject()
360: || FileUtil.isParentOf(root,
361: getQueriedFileObject());
362: }
363: isInProjectJavaRoot = Boolean.valueOf(result);
364: }
365: return isInProjectJavaRoot.booleanValue();
366: }
367:
368: public boolean isInProjectJspRoot() {
369: if (isInProjectJspRoot == null) {
370: FileObject root = getProjectJspRoot();
371: boolean result;
372: if (root == null) {
373: result = false;
374: } else {
375: result = (root == getQueriedFileObject())
376: || (FileUtil.isParentOf(root,
377: getQueriedFileObject()));
378: }
379: isInProjectJspRoot = Boolean.valueOf(result);
380: }
381: return isInProjectJspRoot.booleanValue();
382: }
383:
384: public boolean isTemplateFileObject() {
385: if (isTemplateFileObject == null) {
386: FileObject fileObject = getQueriedFileObject();
387: Object attribute = fileObject
388: .getAttribute(DataObject.PROP_TEMPLATE);
389: boolean hasTemplateAttribute;
390: boolean isTemplate;
391: if (attribute instanceof Boolean) {
392: hasTemplateAttribute = ((Boolean) attribute)
393: .booleanValue();
394: } else {
395: hasTemplateAttribute = false;
396: }
397: if (hasTemplateAttribute) {
398: isTemplate = true;
399: } else {
400: FileObject templatesFolder = Repository.getDefault()
401: .getDefaultFileSystem().getRoot()
402: .getFileObject("Templates"); // NOI18N
403: if (templatesFolder != null) {
404: isTemplate = FileUtil.isParentOf(templatesFolder,
405: fileObject);
406: } else {
407: isTemplate = false;
408: }
409: }
410: isTemplateFileObject = Boolean.valueOf(isTemplate);
411: }
412: return isTemplateFileObject.booleanValue();
413: }
414:
415: }
|