| /******************************************************************************* |
| * Copyright (c) 2008 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.operations; |
| |
| import java.util.Set; |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IFolder; |
| import org.eclipse.core.resources.IProject; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jdt.core.IPackageFragmentRoot; |
| import org.eclipse.jdt.core.JavaCore; |
| import org.eclipse.jem.util.emf.workbench.ProjectUtilities; |
| import org.eclipse.jpt.core.JpaProject; |
| import org.eclipse.jpt.core.JptCorePlugin; |
| import org.eclipse.jpt.core.context.persistence.Persistence; |
| import org.eclipse.jpt.core.context.persistence.PersistenceUnit; |
| import org.eclipse.jpt.core.context.persistence.PersistenceXml; |
| import org.eclipse.jpt.core.internal.JptCoreMessages; |
| import org.eclipse.jpt.core.resource.orm.AccessType; |
| import org.eclipse.jpt.utility.internal.StringTools; |
| import org.eclipse.jst.j2ee.internal.project.J2EEProjectUtilities; |
| import org.eclipse.wst.common.frameworks.datamodel.AbstractDataModelProvider; |
| import org.eclipse.wst.common.frameworks.datamodel.DataModelPropertyDescriptor; |
| import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| import org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation; |
| |
| public class OrmFileCreationDataModelProvider extends AbstractDataModelProvider |
| implements OrmFileCreationDataModelProperties |
| { |
| /** |
| * required default constructor |
| */ |
| public OrmFileCreationDataModelProvider() { |
| super(); |
| } |
| |
| |
| @Override |
| public IDataModelOperation getDefaultOperation() { |
| return new OrmFileCreationOperation(getDataModel()); |
| } |
| |
| @Override |
| public Set<String> getPropertyNames() { |
| @SuppressWarnings("unchecked") |
| Set<String> propertyNames = super.getPropertyNames(); |
| propertyNames.add(PROJECT_NAME); |
| propertyNames.add(SOURCE_FOLDER); |
| propertyNames.add(FILE_PATH); |
| propertyNames.add(DEFAULT_ACCESS); |
| propertyNames.add(ADD_TO_PERSISTENCE_UNIT); |
| propertyNames.add(PERSISTENCE_UNIT); |
| return propertyNames; |
| } |
| |
| @Override |
| public Object getDefaultProperty(String propertyName) { |
| if (propertyName.equals(SOURCE_FOLDER)) { |
| IFolder sourceFolder = getDefaultSourceFolder(); |
| if (sourceFolder != null && sourceFolder.exists()) { |
| return sourceFolder.getFullPath().toOSString(); |
| } |
| } |
| else if (propertyName.equals(FILE_PATH)) { |
| return new Path(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH).toOSString(); |
| } |
| else if (propertyName.equals(DEFAULT_ACCESS)) { |
| return null; |
| } |
| else if (propertyName.equals(ADD_TO_PERSISTENCE_UNIT)) { |
| return Boolean.FALSE; |
| } |
| else if (propertyName.equals(PERSISTENCE_UNIT)) { |
| PersistenceUnit pUnit = getDefaultPersistenceUnit(); |
| if (pUnit != null) { |
| return pUnit.getName(); |
| } |
| } |
| return super.getDefaultProperty(propertyName); |
| } |
| |
| @Override |
| public boolean propertySet(String propertyName, Object propertyValue) { |
| boolean ok = super.propertySet(propertyName, propertyValue); |
| if (propertyName.equals(PROJECT_NAME)) { |
| this.model.notifyPropertyChange(SOURCE_FOLDER, IDataModel.DEFAULT_CHG); |
| } |
| return ok; |
| } |
| |
| @Override |
| public DataModelPropertyDescriptor[] getValidPropertyDescriptors(String propertyName) { |
| if (propertyName.equals(DEFAULT_ACCESS)) { |
| DataModelPropertyDescriptor[] accessTypes = new DataModelPropertyDescriptor[3]; |
| accessTypes[0] = new DataModelPropertyDescriptor(null, JptCoreMessages.NONE); |
| accessTypes[1] = new DataModelPropertyDescriptor(AccessType.FIELD, AccessType.FIELD.getName()); |
| accessTypes[2] = new DataModelPropertyDescriptor(AccessType.PROPERTY, AccessType.PROPERTY.getName()); |
| return accessTypes; |
| } |
| else { |
| return super.getValidPropertyDescriptors(propertyName); |
| } |
| } |
| |
| |
| // **************** validation ********************************************* |
| |
| @Override |
| public IStatus validate(String propertyName) { |
| if (propertyName.equals(PROJECT_NAME) |
| || propertyName.equals(SOURCE_FOLDER) |
| || propertyName.equals(FILE_PATH)) { |
| return validateProjectSourceFolderAndFilePath(); |
| } |
| else if (propertyName.equals(ADD_TO_PERSISTENCE_UNIT) |
| || propertyName.equals(PERSISTENCE_UNIT)) { |
| return validatePersistenceUnit(); |
| } |
| return super.validate(propertyName); |
| } |
| |
| private IStatus validateProjectSourceFolderAndFilePath() { |
| String projectName = (String) getProperty(PROJECT_NAME); |
| if (StringTools.stringIsEmpty(projectName)) { |
| return new Status( |
| IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.VALIDATE_PROJECT_NOT_SPECIFIED); |
| } |
| String sourceFolderPath = getStringProperty(SOURCE_FOLDER); |
| if (StringTools.stringIsEmpty(sourceFolderPath)) { |
| return new Status( |
| IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_SPECIFIED); |
| } |
| if (getVerifiedSourceFolder() == null) { |
| return new Status( |
| IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.bind(JptCoreMessages.VALIDATE_SOURCE_FOLDER_DOES_NOT_EXIST, sourceFolderPath)); |
| } |
| if (getVerifiedSourceFolder().getProject() != getProject()) { |
| return new Status( |
| IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.bind( |
| JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_IN_PROJECT, |
| sourceFolderPath, projectName)); |
| } |
| if (getVerifiedJavaSourceFolder() == null) { |
| return new Status( |
| IStatus.WARNING, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.bind(JptCoreMessages.VALIDATE_SOURCE_FOLDER_NOT_SOURCE_FOLDER, sourceFolderPath)); |
| } |
| if (getExistingOrmFile() != null) { |
| return new Status( |
| IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.VALIDATE_ORM_FILE_ALREADY_EXISTS); |
| } |
| return Status.OK_STATUS; |
| } |
| |
| private IStatus validatePersistenceUnit() { |
| boolean addToPUnit = getBooleanProperty(ADD_TO_PERSISTENCE_UNIT); |
| String pUnitName = getStringProperty(PERSISTENCE_UNIT); |
| if (addToPUnit) { |
| if (getPersistenceUnit() == null) { |
| return new Status( |
| IStatus.ERROR, JptCorePlugin.PLUGIN_ID, |
| JptCoreMessages.bind(JptCoreMessages.VALIDATE_PERSISTENCE_UNIT_DOES_NOT_EXIST, pUnitName)); |
| } |
| } |
| return Status.OK_STATUS; |
| } |
| |
| |
| // **************** helper methods ***************************************** |
| |
| // Copied from ArtifactEditOperationDataModelProvider |
| private IProject getProject() { |
| String projectName = (String) model.getProperty(PROJECT_NAME); |
| if (StringTools.stringIsEmpty(projectName)) { |
| return null; |
| } |
| return ProjectUtilities.getProject(projectName); |
| } |
| |
| private JpaProject getJpaProject() { |
| IProject project = getProject(); |
| if (project == null) { |
| return null; |
| } |
| return JptCorePlugin.getJpaProject(project); |
| } |
| |
| /** |
| * Return a best guess java source folder for the specified project |
| */ |
| // Copied from NewJavaClassDataModelProvider |
| private IFolder getDefaultSourceFolder() { |
| IProject project = getProject(); |
| if (project == null) { |
| return null; |
| } |
| IPackageFragmentRoot[] sources = J2EEProjectUtilities.getSourceContainers(project); |
| // Try and return the first source folder |
| if (sources.length > 0) { |
| try { |
| return (IFolder) sources[0].getCorrespondingResource(); |
| } catch (Exception e) { |
| return null; |
| } |
| } |
| return null; |
| } |
| |
| /** |
| * Return an IFolder represented by the SOURCE_FOLDER property, verified |
| * to exist |
| */ |
| private IFolder getVerifiedSourceFolder() { |
| String folderPath = getStringProperty(SOURCE_FOLDER); |
| IProject project = getProject(); |
| if (project == null) { |
| return null; |
| } |
| IFolder folder; |
| try { |
| folder = project.getWorkspace().getRoot().getFolder(new Path(folderPath)); |
| } |
| catch (IllegalArgumentException e) { |
| return null; |
| } |
| if (folder == null || ! folder.exists()) { |
| return null; |
| } |
| return folder; |
| } |
| |
| /** |
| * Return the source folder, provided it is verified to be an actual java |
| * source folder |
| */ |
| private IFolder getVerifiedJavaSourceFolder() { |
| IFolder folder = getVerifiedSourceFolder(); |
| if (folder == null) { |
| return null; |
| } |
| IJavaProject jProject = JavaCore.create(getProject()); |
| if (jProject == null) { |
| return null; |
| } |
| IPackageFragmentRoot packageFragmentRoot = jProject.getPackageFragmentRoot(folder); |
| if (packageFragmentRoot == null) { |
| return null; |
| } |
| return folder; |
| } |
| |
| private IFile getExistingOrmFile() { |
| IFolder folder = getVerifiedSourceFolder(); |
| if (folder == null) { |
| return null; |
| } |
| String filePath = getStringProperty(FILE_PATH); |
| IFile existingFile = folder.getFile(new Path(filePath)); |
| if (! existingFile.exists()) { |
| return null; |
| } |
| return existingFile; |
| } |
| |
| private PersistenceUnit getDefaultPersistenceUnit() { |
| JpaProject jpaProject = getJpaProject(); |
| if (jpaProject == null) { |
| return null; |
| } |
| PersistenceXml persistenceXml = jpaProject.getRootContext().getPersistenceXml(); |
| if (persistenceXml == null) { |
| return null; |
| } |
| Persistence persistence = persistenceXml.getPersistence(); |
| if (persistence == null) { |
| return null; |
| } |
| if (persistence.persistenceUnitsSize() == 0) { |
| return null; |
| } |
| return persistence.persistenceUnits().next(); |
| } |
| |
| private PersistenceUnit getPersistenceUnit() { |
| String pUnitName = getStringProperty(PERSISTENCE_UNIT); |
| if (StringTools.stringIsEmpty(pUnitName)) { |
| return null; |
| } |
| JpaProject jpaProject = getJpaProject(); |
| if (jpaProject == null) { |
| return null; |
| } |
| PersistenceXml persistenceXml = jpaProject.getRootContext().getPersistenceXml(); |
| if (persistenceXml == null) { |
| return null; |
| } |
| PersistenceUnit pUnit = persistenceXml.getPersistenceUnit(); |
| if (! pUnitName.equals(pUnit.getName())) { |
| return null; |
| } |
| return pUnit; |
| } |
| } |