### Eclipse Workspace Patch 1.0 #P org.teiid.designer.roles.ui Index: src/org/teiid/designer/roles/ui/DataRolesModelTreeProvider.java =================================================================== --- src/org/teiid/designer/roles/ui/DataRolesModelTreeProvider.java (revision 1772) +++ src/org/teiid/designer/roles/ui/DataRolesModelTreeProvider.java (working copy) @@ -11,7 +11,6 @@ import java.util.Collection; import java.util.List; import java.util.Map; - import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; import org.eclipse.emf.ecore.EObject; @@ -25,7 +24,6 @@ import org.teiid.core.util.StringUtil; import org.teiid.designer.roles.Crud; import org.teiid.designer.roles.Permission; - import com.metamatrix.core.util.StringUtilities; import com.metamatrix.metamodels.core.ModelAnnotation; import com.metamatrix.metamodels.core.ModelType; @@ -46,137 +44,136 @@ import com.metamatrix.modeler.internal.ui.viewsupport.ModelUtilities; public class DataRolesModelTreeProvider implements ITreeContentProvider, ITableLabelProvider { - private static final Object[] NO_CHILDREN = new Object[0]; - private ITreeContentProvider modelProvider = ModelUtilities.getModelContentProvider(); - private static final char DELIM = StringUtil.Constants.DOT_CHAR; - private static final char B_SLASH = '/'; - - private static final Image CHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.CHECKED_BOX_ICON); - private static final Image UNCHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.UNCHECKED_BOX_ICON); - private static final Image GRAY_CHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.GRAY_CHECKED_BOX_ICON); - private static final Image GRAY_UNCHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.GRAY_UNCHECKED_BOX_ICON); - -// private Map permissionsMap; - private PermissionHandler handler; - - private Resource[] resources; - - public DataRolesModelTreeProvider(Map permissionsMap) { - super(); - handler = new PermissionHandler(this, permissionsMap); - } + private static final Object[] NO_CHILDREN = new Object[0]; + private ITreeContentProvider modelProvider = ModelUtilities.getModelContentProvider(); + private static final char DELIM = StringUtil.Constants.DOT_CHAR; + private static final char B_SLASH = '/'; - @Override - public void addListener(ILabelProviderListener listener) { - // NO OP - } - - @Override - public void dispose() { - // NO OP - } + private static final Image CHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.CHECKED_BOX_ICON); + private static final Image UNCHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.UNCHECKED_BOX_ICON); + private static final Image GRAY_CHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.GRAY_CHECKED_BOX_ICON); + private static final Image GRAY_UNCHECKED_BOX = RolesUiPlugin.getInstance().getAnImage(RolesUiConstants.Images.GRAY_UNCHECKED_BOX_ICON); - /* - * Helper method to find a check-box image based on boolean value, if it is a parent or not and if it has at lease one - * child (or grand-child, etc.) that has a different boolean value. - */ + // private Map permissionsMap; + private PermissionHandler handler; + + private Resource[] resources; + + public DataRolesModelTreeProvider( Map permissionsMap ) { + super(); + handler = new PermissionHandler(this, permissionsMap); + } + + @Override + public void addListener( ILabelProviderListener listener ) { + // NO OP + } + + @Override + public void dispose() { + // NO OP + } + + /* + * Helper method to find a check-box image based on boolean value, if it is a parent or not and if it has at lease one + * child (or grand-child, etc.) that has a different boolean value. + */ private Image getCheckBoxImage(Boolean value, boolean isParentValue, boolean hasDifferentChildValue) { - // First case where the actual node does not have a CRUD value, so the first parent with a Value determines the state - // of the check-box along with the boolean indicating that a child below it has a different value - if( isParentValue ) { - if( value == Boolean.FALSE) return GRAY_UNCHECKED_BOX; - if( hasDifferentChildValue ) return GRAY_CHECKED_BOX; - return CHECKED_BOX; - } - - // The case where the actual node HAS a Permission with a non-null CRUD value - if( value == Boolean.FALSE) return UNCHECKED_BOX; - if( hasDifferentChildValue ) return GRAY_CHECKED_BOX; - return CHECKED_BOX; + // First case where the actual node does not have a CRUD value, so the first parent with a Value determines the state + // of the check-box along with the boolean indicating that a child below it has a different value + if (isParentValue) { + if (value == Boolean.FALSE) return GRAY_UNCHECKED_BOX; + if (hasDifferentChildValue) return GRAY_CHECKED_BOX; + return CHECKED_BOX; + } - } - - @Override - public Object[] getChildren(Object parentElement) { + // The case where the actual node HAS a Permission with a non-null CRUD value + if (value == Boolean.FALSE) return UNCHECKED_BOX; + if (hasDifferentChildValue) return GRAY_CHECKED_BOX; + return CHECKED_BOX; + + } + + @Override + public Object[] getChildren( Object parentElement ) { Object[] children = NO_CHILDREN; - + if (parentElement instanceof EObject) { children = getFilteredModelContents(modelProvider.getChildren(parentElement)); - } else if( parentElement instanceof Resource ) { - children = getFilteredModelContents(((Resource)parentElement).getContents()); + } else if (parentElement instanceof Resource) { + children = getFilteredModelContents(((Resource)parentElement).getContents()); } - return children; - } + return children; + } - @Override + @Override public Image getColumnImage(Object element, int columnIndex) { - - if( columnIndex == 0 ) { - return getImage(element); - } - Crud.Type crudType = Crud.getCrudType(columnIndex); - boolean supportsUpdate = handler.supportsUpdates(element, crudType); - - if( !supportsUpdate ) { - return GRAY_UNCHECKED_BOX; - } - - // Should ALWAYS FIND ONE if it's a RESOURCE (i.e. Model) - Permission perm = handler.getPermission(element); - boolean isParent = false; - + if (columnIndex == 0) { + return getImage(element); + } + + Crud.Type crudType = Crud.getCrudType(columnIndex); + boolean supportsUpdate = handler.supportsUpdates(element, crudType); + + if (!supportsUpdate) { + return GRAY_UNCHECKED_BOX; + } + + // Should ALWAYS FIND ONE if it's a RESOURCE (i.e. Model) + Permission perm = handler.getPermission(element); + boolean isParent = false; + // If no permission exists for this element OR if the element's permission contains a NULL boolean value for this crud type - // then we walk up the tree to find the first one that is NON-NULL - if( perm == null || perm.getCRUDValue(crudType) == null ) { - - perm = handler.getExistingPermission(element, crudType); - isParent = true; - } + // then we walk up the tree to find the first one that is NON-NULL + if (perm == null || perm.getCRUDValue(crudType) == null) { - if( perm != null ) { - Boolean booleanValue = perm.getCRUDValue(crudType); - isParent = perm.isPrimary(); - boolean hasDifferentChildValue = false; + perm = handler.getExistingPermission(element, crudType); + isParent = true; + } + + if (perm != null) { + Boolean booleanValue = perm.getCRUDValue(crudType); + isParent = perm.isPrimary(); + boolean hasDifferentChildValue = false; // If the permission is defined by a parent value, then we assume that this element has a NULL value and we need to determine - // if any children below the element has a permission crud value not equal to the parent. - if( isParent ) { - hasDifferentChildValue = handler.hasChildWithDifferentCrudValue(perm, element, crudType); - } - - return getCheckBoxImage(booleanValue, isParent, hasDifferentChildValue); - } - - return GRAY_UNCHECKED_BOX; - } + // if any children below the element has a permission crud value not equal to the parent. + if (isParent) { + hasDifferentChildValue = handler.hasChildWithDifferentCrudValue(perm, element, crudType); + } - @Override + return getCheckBoxImage(booleanValue, isParent, hasDifferentChildValue); + } + + return GRAY_UNCHECKED_BOX; + } + + @Override public String getColumnText(Object element, int columnIndex) { - if( columnIndex == 0 ) { - return getText(element); - } - return null; - } + if (columnIndex == 0) { + return getText(element); + } + return null; + } + @Override + public Object[] getElements( Object inputElement ) { + if (resources == null) { + resources = (Resource[])((ContainerImpl)inputElement).getResources().toArray(); + } - @Override - public Object[] getElements(Object inputElement) { - if( resources == null ) { - resources = (Resource[])((ContainerImpl)inputElement).getResources().toArray(); - } - - reInitializePermissions(); - return resources; - } + reInitializePermissions(); + return resources; + } - /* - * return an array of objects that are filtered for only Relational Model object types. - */ - private Object[] getFilteredModelContents(List eObjs) { - Collection relObjects = new ArrayList(); - for( EObject eObj : eObjs ) { + /* + * return an array of objects that are filtered for only Relational Model object types. + */ + private Object[] getFilteredModelContents( List eObjs ) { + Collection relObjects = new ArrayList(); + for (EObject eObj : eObjs) { if( eObj instanceof Table || eObj instanceof View || eObj instanceof Procedure || @@ -187,19 +184,19 @@ eObj instanceof Interface || eObj instanceof Operation || eObj instanceof XmlDocument) { - relObjects.add(eObj); - } - } - - return relObjects.toArray(); - } - - /* - * return an array of objects that are filtered for only Relational Model object types. - */ - private Object[] getFilteredModelContents(Object[] eObjs) { - Collection relObjects = new ArrayList(); - for( Object eObj : eObjs ) { + relObjects.add(eObj); + } + } + + return relObjects.toArray(); + } + + /* + * return an array of objects that are filtered for only Relational Model object types. + */ + private Object[] getFilteredModelContents( Object[] eObjs ) { + Collection relObjects = new ArrayList(); + for (Object eObj : eObjs) { if( eObj instanceof Table || eObj instanceof View || eObj instanceof Procedure || @@ -210,190 +207,200 @@ eObj instanceof Interface || eObj instanceof Operation || eObj instanceof XmlDocument) { - relObjects.add((EObject)eObj); - } - } - - return relObjects.toArray(); - } - - /* - * Returns an image for the element. Targeted for Column 1 in the Tree corresponding to the Model and it's children. - * - */ - private Image getImage(Object element) { + relObjects.add((EObject)eObj); + } + } + + return relObjects.toArray(); + } + + /* + * Returns an image for the element. Targeted for Column 1 in the Tree corresponding to the Model and it's children. + * + */ + private Image getImage( Object element ) { if (element instanceof EObject) { return ModelUtilities.getEMFLabelProvider().getImage(element); } else if (element instanceof Resource) { - try { - EObject firstEObj = ((Resource)element).getContents().get(0); - ModelAnnotation ma = ModelerCore.getModelEditor().getModelAnnotation(firstEObj); - ModelType mType = ma.getModelType(); - if( ModelType.PHYSICAL_LITERAL == mType ) { - return ModelIdentifier.getImage(ModelIdentifier.RELATIONAL_SOURCE_MODEL_ID); - } - return ModelIdentifier.getImage(ModelIdentifier.RELATIONAL_VIEW_MODEL_ID); - } catch (ModelerCoreException e) { - RolesUiPlugin.UTIL.log(IStatus.ERROR, RolesUiPlugin.UTIL.getString("errorFindingImageForObject", element, e)); //$NON-NLS-1$ - } + try { + EObject firstEObj = ((Resource)element).getContents().get(0); + ModelAnnotation ma = ModelerCore.getModelEditor().getModelAnnotation(firstEObj); + ModelType mType = ma.getModelType(); + if (ModelType.PHYSICAL_LITERAL == mType) { + return ModelIdentifier.getImage(ModelIdentifier.RELATIONAL_SOURCE_MODEL_ID); + } + return ModelIdentifier.getImage(ModelIdentifier.RELATIONAL_VIEW_MODEL_ID); + } catch (ModelerCoreException e) { + RolesUiPlugin.UTIL.log(IStatus.ERROR, RolesUiPlugin.UTIL.getString("errorFindingImageForObject", element, e)); //$NON-NLS-1$ + } } - + return null; - } - - @Override - public Object getParent(Object element) { - if( element instanceof EObject ) { - return ModelUtilities.getModelContentProvider().getParent(element); - } else if( element instanceof Resource ) { - return ((Resource)element).getResourceSet(); - } - return null; - } - - /** - * Returns a string target name for the specified element + } + + @Override + public Object getParent( Object element ) { + if (element instanceof EObject) { + return ModelUtilities.getModelContentProvider().getParent(element); + } else if (element instanceof Resource) { + return ((Resource)element).getResourceSet(); + } + return null; + } + + /** + * Returns a string target name for the specified element * - */ - public String getTargetName(Object element) { - String targetName = null; - if( element instanceof Resource ) { - return getResourceName((Resource)element); - } else if( element instanceof EObject ) { - EObject eObj = (EObject)element; - targetName = getResourceName(eObj.eResource()) + '/' + ModelerCore.getModelEditor().getModelRelativePath(eObj); - } - - targetName = targetName.replace(B_SLASH, DELIM); - - return targetName; - } - - /** - * Returns an actual Model Object (EObject or Resource) for the specified Permission + */ + public String getTargetName( Object element ) { + String targetName = null; + if (element instanceof Resource) { + return getResourceName((Resource)element); + } else if (element instanceof EObject) { + EObject eObj = (EObject)element; + targetName = getResourceName(eObj.eResource()) + '/' + ModelerCore.getModelEditor().getModelRelativePath(eObj); + } + + targetName = targetName.replace(B_SLASH, DELIM); + + return targetName; + } + + /** + * Returns an actual Model Object (EObject or Resource) for the specified Permission + * * - * - * @param perm - * @return - */ - public Object getPermissionTargetObject(Permission perm) { - String targetName = perm.getTargetName(); - for( Resource res : resources ) { - if( getTargetName(res).equals(targetName) ) { - return res; - } + * @param perm + * @return + */ + public Object getPermissionTargetObject( Permission perm ) { + String targetName = perm.getTargetName(); + for (Resource res : resources) { + if (getTargetName(res).equals(targetName)) { + return res; + } - for( Object child : getChildren(res) ) { - if( getTargetName(child).equals(targetName)) { - return child; - } - Object target = getTargetObjectInChildren(child, targetName); - if( target != null ) { - return target; - } - } - } - - return null; - } + for (Object child : getChildren(res)) { + if (getTargetName(child).equals(targetName)) { + return child; + } + Object target = getTargetObjectInChildren(child, targetName); + if (target != null) { + return target; + } + } + } - /* - * Returns the file name only minus the xmi file extension - */ - private String getResourceName(Resource res) { - - if( res.getURI().path().endsWith(".xmi")) { //$NON-NLS-1$ - Path path = new Path(res.getURI().path()); - return path.removeFileExtension().lastSegment(); - } else { - return res.getURI().path(); - } - } + return null; + } - /* - * Finds the target object under the specified parent. May return null. - * - */ + /* + * Returns the file name only minus the xmi file extension + */ + private String getResourceName( Resource res ) { + + if (res.getURI().path().endsWith(".xmi")) { //$NON-NLS-1$ + Path path = new Path(res.getURI().path()); + return path.removeFileExtension().lastSegment(); + } + return res.getURI().path(); + } + + /* + * Finds the target object under the specified parent. May return null. + * + */ private Object getTargetObjectInChildren(Object parent, String targetName) { - for( Object child : getChildren(parent) ) { - if( getTargetName(child).equals(targetName) ) { - return child; - } - Object target = getTargetObjectInChildren(child, targetName); - if( target != null ) { - return target; - } - } - - return null; - } + for (Object child : getChildren(parent)) { + if (getTargetName(child).equals(targetName)) { + return child; + } + Object target = getTargetObjectInChildren(child, targetName); + if (target != null) { + return target; + } + } - /* - * Helper method to retrieve the text value for an EMF object (EObject or Resource) - * - */ - private String getText(Object element) { - if (element instanceof EObject) { + return null; + } + + /* + * Helper method to retrieve the text value for an EMF object (EObject or Resource) + * + */ + private String getText( Object element ) { + if (element instanceof EObject) { ILabelProvider p = ModelUtilities.getEMFLabelProvider(); return p.getText(element); } else if (element instanceof Resource) { - return ((Resource)element).getURI().lastSegment(); + return ((Resource)element).getURI().lastSegment(); } - - return StringUtilities.EMPTY_STRING; - } - - @Override - public boolean hasChildren(Object element) { - return getChildren(element).length > 0; - } - @Override + return StringUtilities.EMPTY_STRING; + } + + @Override + public boolean hasChildren( Object element ) { + return getChildren(element).length > 0; + } + + @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - // NO OP - - } - - @Override + // NO OP + + } + + @Override public boolean isLabelProperty(Object element, String property) { - // NO OP - return false; - } - - /* - * Make sure that ALL resources have a permissions object?? - */ - private void reInitializePermissions() { - - if( handler.hasPermissions() ) { - return; - } - - for( Resource res : resources ) { - String resPath = getTargetName(res); - Permission perm = new Permission(resPath, false, true, false, false); - perm.setPrimary(true); - handler.addPermission(res, perm); - } - } - - @Override - public void removeListener(ILabelProviderListener listener) { - // NO OP - } - - /** + // NO OP + return false; + } + + /* + * Make sure that ALL resources have a permissions object?? + */ + private void reInitializePermissions() { + + if (handler.hasPermissions()) { + return; + } + + for (Resource res : resources) { + String resPath = getTargetName(res); + Permission perm = new Permission(resPath, false, true, false, false); + perm.setPrimary(true); + handler.addPermission(res, perm); + } + } + + @Override + public void removeListener( ILabelProviderListener listener ) { + // NO OP + } + + /** * Performs the necessary permission CRUD value changes based on the target element and the CRUD type. * This method is targeted for use by a single-click editor changing ONE CRUD boolean value for one object. - * - * @param element - * @param crudType - */ + * + * @param element + * @param crudType + */ public void togglePermission( Object element, Crud.Type crudType ) { - handler.togglePermission(element, crudType); - } - + handler.togglePermission(element, crudType); + } + /** + * Get any warnings which we may need to show the user, before toggling. + * + * @param element + * @param crudType + * @return IStatus status toggle warnings to display, if any + */ + public IStatus getToggleStatus( Object element, + Crud.Type crudType ) { + return handler.getToggleStatus(element, crudType); + } + } Index: src/org/teiid/designer/roles/ui/NewDataRoleWizard.java =================================================================== --- src/org/teiid/designer/roles/ui/NewDataRoleWizard.java (revision 1772) +++ src/org/teiid/designer/roles/ui/NewDataRoleWizard.java (working copy) @@ -12,8 +12,10 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.eclipse.core.runtime.IStatus; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.text.DocumentEvent; import org.eclipse.jface.text.IDocumentListener; @@ -63,7 +65,7 @@ private static final String EDIT_TITLE = getString("editTitle"); //$NON-NLS-1$ private static final ImageDescriptor IMAGE = RolesUiPlugin.getInstance().getImageDescriptor("icons/full/wizban/dataPolicyWizard.png"); //$NON-NLS-1$ - + private static final String DEFAULT_NAME = "Data Role 1"; //getString("undefinedName"); //$NON-NLS-1$ private static final String SYS_ADMIN_TABLE_TARGET = "sysadmin"; //$NON-NLS-1$ private static final String SYS_TABLE_TARGET = "sys"; //$NON-NLS-1$ @@ -71,10 +73,10 @@ private static String getString( final String id ) { return RolesUiPlugin.UTIL.getString(I18N_PREFIX + id); } - + private DataRole dataRole; private Container tempContainer; - + private boolean isEdit = false; private WizardPage wizardPage; private Text dataRoleNameText; @@ -86,7 +88,7 @@ private Button allowCreateTempTablesCheckBox; private DataRolesModelTreeProvider treeProvider; - + private String dataRoleName; private String description; private Set mappedRoleNames; @@ -95,66 +97,64 @@ private boolean allowCreateTempTables; String roleNameTextEntry; - + private Map permissionsMap; - - /** + + /** * @since 4.0 */ public NewDataRoleWizard(Container tempContainer, DataRole existingDataRole) { - super(RolesUiPlugin.getInstance(), TITLE, IMAGE); - this.tempContainer = tempContainer; - if( existingDataRole == null ) { - this.dataRole = new DataRole(DEFAULT_NAME); - this.dataRoleName = DEFAULT_NAME; - this.isEdit = false; - this.allowSystemTables = true; - this.anyAuthentication = false; - this.allowCreateTempTables = false; - } else { - this.dataRole = existingDataRole; - this.isEdit = true; - this.setWindowTitle(EDIT_TITLE); - } + super(RolesUiPlugin.getInstance(), TITLE, IMAGE); + this.tempContainer = tempContainer; + if (existingDataRole == null) { + this.dataRole = new DataRole(DEFAULT_NAME); + this.dataRoleName = DEFAULT_NAME; + this.isEdit = false; + this.allowSystemTables = true; + this.anyAuthentication = false; + this.allowCreateTempTables = false; + } else { + this.dataRole = existingDataRole; + this.isEdit = true; + this.setWindowTitle(EDIT_TITLE); + } this.mappedRoleNames = new HashSet(dataRole.getRoleNames()); this.permissionsMap = new HashMap(); } /** - * - * * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection) */ public void init( IWorkbench workbench, IStructuredSelection selection ) { - this.wizardPage = new WizardPage(NewDataRoleWizard.class.getSimpleName(), TITLE, null) { + this.wizardPage = new WizardPage(NewDataRoleWizard.class.getSimpleName(), TITLE, null) { + @Override public void createControl( final Composite parent ) { setControl(createPageControl(parent)); } }; - + this.wizardPage.setPageComplete(false); - if( isEdit ) { - this.wizardPage.setMessage(getString("initialEditMessage")); //$NON-NLS-1$ - this.wizardPage.setTitle(EDIT_TITLE); + if (isEdit) { + this.wizardPage.setMessage(getString("initialEditMessage")); //$NON-NLS-1$ + this.wizardPage.setTitle(EDIT_TITLE); } else { - this.wizardPage.setMessage(getString("initialCreateMessage")); //$NON-NLS-1$ + this.wizardPage.setMessage(getString("initialCreateMessage")); //$NON-NLS-1$ } addPage(wizardPage); - + } - + /** - * @param parent + * @param parent * @return composite the page * @since 4.0 */ - Composite createPageControl( final Composite parent ) { - // Tree Content Provider - - treeProvider = new DataRolesModelTreeProvider(this.permissionsMap); - - + Composite createPageControl( final Composite parent ) { + // Tree Content Provider + + treeProvider = new DataRolesModelTreeProvider(this.permissionsMap); + // ===========>>>> Create page composite final Composite mainPanel = new Composite(parent, SWT.NONE); GridData pgGD = new GridData(GridData.FILL_BOTH); @@ -162,17 +162,18 @@ mainPanel.setLayout(new GridLayout(2, false)); // Add widgets to page WidgetFactory.createLabel(mainPanel, getString("nameLabel")); //$NON-NLS-1$ - + this.dataRoleNameText = WidgetFactory.createTextField(mainPanel, GridData.FILL_HORIZONTAL, 1, DEFAULT_NAME); - + this.dataRoleNameText.addModifyListener(new ModifyListener() { - public void modifyText(ModifyEvent e) { - dataRoleName = dataRoleNameText.getText(); - validateInputs(); - } - }); - - // ===========>>>> Create Description Group + @Override + public void modifyText( ModifyEvent e ) { + dataRoleName = dataRoleNameText.getText(); + validateInputs(); + } + }); + + // ===========>>>> Create Description Group final Group descGroup = WidgetFactory.createGroup(mainPanel, getString("desciptionGroupLabel"), GridData.FILL_HORIZONTAL, 2); //$NON-NLS-1$ descriptionTextEditor = new StyledTextEditor(descGroup, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.WRAP); final GridData descGridData = new GridData(GridData.FILL_BOTH); @@ -183,51 +184,50 @@ descriptionTextEditor.setLayoutData(descGridData); descriptionTextEditor.setText(""); //$NON-NLS-1$ descriptionTextEditor.getDocument().addDocumentListener(new IDocumentListener() { - - @Override - public void documentChanged(DocumentEvent event) { - description = descriptionTextEditor.getText(); - } - - @Override - public void documentAboutToBeChanged(DocumentEvent event) { - // NO OP - } - }); - + + @Override + public void documentChanged( DocumentEvent event ) { + description = descriptionTextEditor.getText(); + } + + @Override + public void documentAboutToBeChanged( DocumentEvent event ) { + // NO OP + } + }); + allowCreateTempTablesCheckBox = WidgetFactory.createCheckBox(mainPanel, - getString("allowCreateTempTablesCheckBox.label"), 0, 2, anyAuthentication); //$NON-NLS-1$ + getString("allowCreateTempTablesCheckBox.label"), 0, 2, anyAuthentication); //$NON-NLS-1$ allowCreateTempTablesCheckBox.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected( final SelectionEvent event ) { - allowCreateTempTables = allowCreateTempTablesCheckBox.getSelection(); - } - }); - + @Override + public void widgetSelected( final SelectionEvent event ) { + allowCreateTempTables = allowCreateTempTablesCheckBox.getSelection(); + } + }); anyAuthenticatedCheckBox = WidgetFactory.createCheckBox(mainPanel, - getString("anyAuthenticatedCheckbox.label"), 0, 2, anyAuthentication); //$NON-NLS-1$ + getString("anyAuthenticatedCheckbox.label"), 0, 2, anyAuthentication); //$NON-NLS-1$ anyAuthenticatedCheckBox.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected( final SelectionEvent event ) { - anyAuthentication = anyAuthenticatedCheckBox.getSelection(); - if( mappedRolesPanel != null ) { - mappedRolesPanel.setEnabled(!anyAuthentication); - } - } - }); - - // ===========>>>> Create Roles List Panel Editor + @Override + public void widgetSelected( final SelectionEvent event ) { + anyAuthentication = anyAuthenticatedCheckBox.getSelection(); + if (mappedRolesPanel != null) { + mappedRolesPanel.setEnabled(!anyAuthentication); + } + } + }); + + // ===========>>>> Create Roles List Panel Editor final IListPanelController ctrlr = new ListPanelAdapter() { @Override public Object[] addButtonSelected() { String title = getString("roleInputDialog.title"); //$NON-NLS-1$ String label = getString("roleInputDialog.label"); //$NON-NLS-1$ String textEntry = showTextEntryDialog(title, label, null); - if( textEntry != null && textEntry.length() > 0 ) { - mappedRoleNames.add(textEntry); + if (textEntry != null && textEntry.length() > 0) { + mappedRoleNames.add(textEntry); } else { - return Collections.EMPTY_LIST.toArray(); + return Collections.EMPTY_LIST.toArray(); } return new String[] {textEntry}; } @@ -235,42 +235,42 @@ @Override public Object[] removeButtonSelected( IStructuredSelection selection ) { Object[] objArray = selection.toArray(); - for( Object obj : objArray ) { - mappedRoleNames.remove(obj); + for (Object obj : objArray) { + mappedRoleNames.remove(obj); } return objArray; } - + @Override public Object editButtonSelected( IStructuredSelection selection ) { Object[] objArray = selection.toArray(); String title = getString("roleInputDialog.title"); //$NON-NLS-1$ String label = getString("roleInputDialog.label"); //$NON-NLS-1$ String textEntry = showTextEntryDialog(title, label, (String)objArray[0]); - if( textEntry != null && textEntry.length() > 0 ) { - mappedRoleNames.add(textEntry); - }else { - return null; + if (textEntry != null && textEntry.length() > 0) { + mappedRoleNames.add(textEntry); + } else { + return null; } - + return textEntry; } - - @Override - public void itemsSelected(final IStructuredSelection selection) { - Object[] objArray = selection.toArray(); - boolean enableEdit = true; - if( objArray == null ) { - enableEdit = false; - } else if( objArray.length == 1 ) { - enableEdit = false; - } else { - enableEdit = true; - } - mappedRolesPanel.getButton(ListPanel.EDIT_BUTTON).setEnabled(enableEdit); - } + + @Override + public void itemsSelected( final IStructuredSelection selection ) { + Object[] objArray = selection.toArray(); + boolean enableEdit = true; + if (objArray == null) { + enableEdit = false; + } else if (objArray.length == 1) { + enableEdit = false; + } else { + enableEdit = true; + } + mappedRolesPanel.getButton(ListPanel.EDIT_BUTTON).setEnabled(enableEdit); + } }; - mappedRolesPanel = new ListPanel(mainPanel, getString("dataRolesGroupLabel"), ctrlr, SWT.H_SCROLL | SWT.V_SCROLL, 2); //$NON-NLS-1$ + mappedRolesPanel = new ListPanel(mainPanel, getString("dataRolesGroupLabel"), ctrlr, SWT.H_SCROLL | SWT.V_SCROLL, 2); //$NON-NLS-1$ final GridData rolesGridData = new GridData(GridData.FILL_BOTH); rolesGridData.horizontalSpan = 2; @@ -280,51 +280,50 @@ mappedRolesPanel.setLayoutData(rolesGridData); mappedRolesPanel.getButton(ListPanel.EDIT_BUTTON).setEnabled(false); mappedRolesPanel.getButton(ListPanel.REMOVE_BUTTON).setEnabled(false); - + mappedRolesPanel.setEnabled(!anyAuthentication); - - + // ===========>>>> Create Relational Models Tree Viewer/Editor - Group group = WidgetFactory.createGroup(mainPanel, getString("relationalModelsGroup"), //$NON-NLS-1$ + Group group = WidgetFactory.createGroup(mainPanel, getString("relationalModelsGroup"), //$NON-NLS-1$ GridData.FILL_BOTH, 2, 2); - + final GridData modelsGridData = new GridData(GridData.FILL_BOTH); modelsGridData.horizontalSpan = 2; modelsGridData.heightHint = 220; modelsGridData.minimumHeight = 220; modelsGridData.grabExcessVerticalSpace = true; group.setLayoutData(modelsGridData); - - treeViewer = new TreeViewer(group, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); + + treeViewer = new TreeViewer(group, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); Tree tree = treeViewer.getTree(); - final GridData gridData = new GridData(GridData.FILL_BOTH); //SWT.FILL, SWT.FILL, true, true); + final GridData gridData = new GridData(GridData.FILL_BOTH); // SWT.FILL, SWT.FILL, true, true); gridData.grabExcessHorizontalSpace = true; treeViewer.getControl().setLayoutData(gridData); - + tree.addMouseListener(new MouseListener() { - - @Override - public void mouseUp(MouseEvent e) { - // NO OP - } - - @Override - public void mouseDown(MouseEvent e) { - Point pt = new Point(e.x, e.y); + + @Override + public void mouseUp( MouseEvent e ) { + // NO OP + } + + @Override + public void mouseDown( MouseEvent e ) { + Point pt = new Point(e.x, e.y); if( treeViewer.getCell(pt) != null && treeViewer.getCell(pt).getViewerRow() != null && treeViewer.getCell(pt).getViewerRow().getItem() != null ) { handleSelection((treeViewer.getCell(pt).getColumnIndex()), treeViewer.getCell(pt).getViewerRow().getItem().getData()); - } - } - - @Override - public void mouseDoubleClick(MouseEvent e) { - // NO OP - } - }); + } + } + @Override + public void mouseDoubleClick( MouseEvent e ) { + // NO OP + } + }); + treeViewer.setUseHashlookup(true); /*** Tree table specific code starts ***/ @@ -351,7 +350,7 @@ layout.addColumnData(new ColumnWeightData(10)); layout.addColumnData(new ColumnWeightData(10)); - tree.setLayout(layout); + tree.setLayout(layout); treeViewer.setContentProvider(treeProvider); treeViewer.setLabelProvider(treeProvider); @@ -372,112 +371,125 @@ }); // ===========>>>> If we're in edit mode, load the UI objects with the info from the input dataRole - if( isEdit ) { - loadExistingPermissions(); + if (isEdit) { + loadExistingPermissions(); } - + return mainPanel; } - + private void loadExistingPermissions() { - this.dataRoleName = this.dataRole.getName(); - this.anyAuthentication = this.dataRole.isAnyAuthenticated(); - this.allowCreateTempTables = this.dataRole.allowCreateTempTables(); - this.dataRoleNameText.setText(this.dataRole.getName()); - this.mappedRolesPanel.addItems(this.dataRole.getRoleNames().toArray()); - this.descriptionTextEditor.setText(this.dataRole.getDescription()); - - for( Permission perm : dataRole.getPermissions() ) { - Object obj = treeProvider.getPermissionTargetObject(perm); - if( obj != null ) { - if( obj instanceof Resource ) { - perm.setPrimary(true); - } - this.permissionsMap.put(obj, perm); + this.dataRoleName = this.dataRole.getName(); + this.anyAuthentication = this.dataRole.isAnyAuthenticated(); + this.allowCreateTempTables = this.dataRole.allowCreateTempTables(); + this.dataRoleNameText.setText(this.dataRole.getName()); + this.mappedRolesPanel.addItems(this.dataRole.getRoleNames().toArray()); + this.descriptionTextEditor.setText(this.dataRole.getDescription()); + + for (Permission perm : dataRole.getPermissions()) { + Object obj = treeProvider.getPermissionTargetObject(perm); + if (obj != null) { + if (obj instanceof Resource) { + perm.setPrimary(true); + } + this.permissionsMap.put(obj, perm); } else if (perm.getTargetName().equalsIgnoreCase(SYS_ADMIN_TABLE_TARGET) || perm.getTargetName().equalsIgnoreCase(SYS_TABLE_TARGET)) { // This is for backward compatability allowSystemTables = true; allowSystemTablesCheckBox.setSelection(allowSystemTables); } - } - - this.anyAuthenticatedCheckBox.setSelection(this.anyAuthentication); - this.allowCreateTempTablesCheckBox.setSelection(this.allowCreateTempTables); - this.mappedRolesPanel.setEnabled(!anyAuthentication); - - treeViewer.refresh(); - - validateInputs(); + } + + this.anyAuthenticatedCheckBox.setSelection(this.anyAuthentication); + this.allowCreateTempTablesCheckBox.setSelection(this.allowCreateTempTables); + this.mappedRolesPanel.setEnabled(!anyAuthentication); + + treeViewer.refresh(); + + validateInputs(); } - /* * This method tells the Tree Provider that */ private void handleSelection(int column, Object rowData) { - if( column > 0 ) { - Crud.Type crudType = Crud.getCrudType(column); - this.treeProvider.togglePermission(rowData, crudType); - } - - treeViewer.refresh(); - - validateInputs(); + if (column > 0) { + Crud.Type crudType = Crud.getCrudType(column); + // Determine any info or warning messages that should be issued before toggling + boolean doToggle = true; + IStatus toggleStatus = this.treeProvider.getToggleStatus(rowData, crudType); + // If info is returned, the element cannot be toggled - display info dialog + if (toggleStatus.getSeverity() == IStatus.INFO) { + String title = RolesUiPlugin.UTIL.getString("NewDataRoleWizard.stateChangeInfoDialog.title"); //$NON-NLS-1$ + MessageDialog.openInformation(getShell(), title, toggleStatus.getMessage()); + doToggle = false; + } else if (toggleStatus.getSeverity() == IStatus.WARNING) { + String title = RolesUiPlugin.UTIL.getString("NewDataRoleWizard.stateChangeConfirmDialog.title"); //$NON-NLS-1$ + doToggle = MessageDialog.openConfirm(getShell(), title, toggleStatus.getMessage()); + } + // Toggle the state + if (doToggle) { + this.treeProvider.togglePermission(rowData, crudType); + } + } + + treeViewer.refresh(); + + validateInputs(); } - - /** + + /** * @see org.eclipse.jface.wizard.IWizard#canFinish() * @since 4.0 */ @Override public boolean canFinish() { boolean canFinish = true; - - + return canFinish; } - - @Override - public boolean finish() { - return true; - } + @Override + public boolean finish() { + return true; + } + public DataRole getDataRole() { - if( dataRole != null ) { - dataRole.setName(this.dataRoleName); - dataRole.setAnyAuthenticated(this.anyAuthentication); - dataRole.setAllowCreateTempTables(this.allowCreateTempTables); - dataRole.setDescription(this.description); - dataRole.setPermissions(permissionsMap.values()); - if (allowSystemTables) { + if (dataRole != null) { + dataRole.setName(this.dataRoleName); + dataRole.setAnyAuthenticated(this.anyAuthentication); + dataRole.setAllowCreateTempTables(this.allowCreateTempTables); + dataRole.setDescription(this.description); + dataRole.setPermissions(permissionsMap.values()); + if (allowSystemTables) { dataRole.addPermission(new Permission(SYS_ADMIN_TABLE_TARGET, false, true, false, false)); } - if( !this.anyAuthentication && !mappedRoleNames.isEmpty() ) { - dataRole.setRoleNames(mappedRoleNames); - } else { - dataRole.getRoleNames().clear(); - } - } - return dataRole; - } + if (!this.anyAuthentication && !mappedRoleNames.isEmpty()) { + dataRole.setRoleNames(mappedRoleNames); + } else { + dataRole.getRoleNames().clear(); + } + } + return dataRole; + } - public void setDataRole(DataRole dataRole) { - this.dataRole = dataRole; - } - - private void validateInputs() { - // Check that name != null - if( this.dataRoleName == null || this.dataRoleName.length() == 0 ) { - wizardPage.setErrorMessage(getString("nullNameMessage")); //$NON-NLS-1$ - wizardPage.setPageComplete(false); - } else { - wizardPage.setErrorMessage(null); - wizardPage.setMessage(getString("okToFinishMessage")); //$NON-NLS-1$ - wizardPage.setPageComplete(true); - } - - } - + public void setDataRole( DataRole dataRole ) { + this.dataRole = dataRole; + } + + private void validateInputs() { + // Check that name != null + if (this.dataRoleName == null || this.dataRoleName.length() == 0) { + wizardPage.setErrorMessage(getString("nullNameMessage")); //$NON-NLS-1$ + wizardPage.setPageComplete(false); + } else { + wizardPage.setErrorMessage(null); + wizardPage.setMessage(getString("okToFinishMessage")); //$NON-NLS-1$ + wizardPage.setPageComplete(true); + } + + } + /** * Show a simple text entry dialog and get the result * @@ -487,9 +499,9 @@ */ String showTextEntryDialog( final String title, final String label, - String initialText) { + String initialText ) { // Dialog for string entry - final String text = initialText; + final String text = initialText; Shell shell = this.wizardPage.getShell(); final Dialog dlg = new Dialog(shell, title) { @Override @@ -499,8 +511,8 @@ Group group = WidgetFactory.createGroup(dlgPanel, label, GridData.FILL_BOTH, 1, 1); final Text nameText = WidgetFactory.createTextField(group, GridData.FILL_HORIZONTAL); - if( text != null ) { - nameText.setText(text); + if (text != null) { + nameText.setText(text); } nameText.setSelection(0); nameText.addModifyListener(new ModifyListener() { @@ -522,7 +534,7 @@ final boolean valid = (newName.length() > 0); getButton(IDialogConstants.OK_ID).setEnabled(valid); if (valid) { - roleNameTextEntry = nameText.getText(); + roleNameTextEntry = nameText.getText(); } } }; Index: src/org/teiid/designer/roles/ui/i18n.properties =================================================================== --- src/org/teiid/designer/roles/ui/i18n.properties (revision 1772) +++ src/org/teiid/designer/roles/ui/i18n.properties (working copy) @@ -30,5 +30,12 @@ NewDataRoleWizard.systemTablesCheckbox.label= Allow this role to access SYSADMIN model NewDataRoleWizard.anyAuthenticatedCheckbox.label= Apply this role to All Users NewDataRoleWizard.allowCreateTempTablesCheckBox.label= Allow usage of temporary tables +NewDataRoleWizard.stateChangeInfoDialog.title=State Change Info +NewDataRoleWizard.stateChangeConfirmDialog.title=Confirm State Change -errorFindingImageForObject=Error finding image for model object {0} \ No newline at end of file +errorFindingImageForObject=Error finding image for model object {0} + +PermissionHandler.infoNotUpdatable=Cannot change Element state - it does not support updates. +PermissionHandler.infoParentNotEnabled=Cannot change Element state - the Parent element must be enabled first. +PermissionHandler.warningWillToggleChildren=Changing the element state will change the state of all children. \n\nContinue? +PermissionHandler.warningWillToggleChildrenIfUpdatable=Changing the element state will change the state of all children,\nif the children support updates. \n\nContinue? Index: src/org/teiid/designer/roles/ui/PermissionHandler.java =================================================================== --- src/org/teiid/designer/roles/ui/PermissionHandler.java (revision 1772) +++ src/org/teiid/designer/roles/ui/PermissionHandler.java (working copy) @@ -10,285 +10,459 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Map; - +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.emf.ecore.EObject; import org.teiid.designer.roles.Crud; +import org.teiid.designer.roles.Crud.Type; import org.teiid.designer.roles.Permission; -import org.teiid.designer.roles.Crud.Type; - import com.metamatrix.metamodels.relational.ProcedureParameter; import com.metamatrix.modeler.core.metamodel.aspect.sql.SqlTableAspect; import com.metamatrix.modeler.internal.transformation.util.TransformationHelper; public class PermissionHandler { - private DataRolesModelTreeProvider tree; - private Map permissionsMap; - + private DataRolesModelTreeProvider tree; + private Map permissionsMap; + public PermissionHandler(DataRolesModelTreeProvider tree, Map permissionsMap) { - super(); - this.tree = tree; - this.permissionsMap = permissionsMap; - } - - /* - * Because child permission values can default to parent Crud values there may be instances where a permission's CRUD - * either exactly matches values from parents, or the CRUD is all NULL. In both cases we need to remove these Permissions - * from the cache. - * - */ - private void cleanUpPermissions(Object changedElement) { - - // walk the tree, - Permission perm = permissionsMap.get(changedElement); - if( perm == null || perm.isPrimary() ) { - return; - } - - if( perm.isNullCrud() ) { - //System.out.println(" Removing Stale Permission for: " + perm.getTargetName()); - permissionsMap.remove(changedElement); //stalePermissionKeys.add(changedElement); - } else { - boolean sameCreate = false; - boolean sameRead = false; - boolean sameUpdate = false; - boolean sameDelete = false; - // look at all 4 CRUD values and their Parent Perms and if they are the same as the CRUD value - Permission parentPerm = getFirstParentPermission(changedElement, Crud.Type.CREATE); - if( parentPerm != null ) { + super(); + this.tree = tree; + this.permissionsMap = permissionsMap; + } + + /* + * Because child permission values can default to parent Crud values there may be instances where a permission's CRUD + * either exactly matches values from parents, or the CRUD is all NULL. In both cases we need to remove these Permissions + * from the cache. + * + */ + private void cleanUpPermissions( Object changedElement ) { + + // walk the tree, + Permission perm = permissionsMap.get(changedElement); + if (perm == null || perm.isPrimary()) { + return; + } + + if (perm.isNullCrud()) { + // System.out.println(" Removing Stale Permission for: " + perm.getTargetName()); + permissionsMap.remove(changedElement); // stalePermissionKeys.add(changedElement); + } else { + boolean sameCreate = false; + boolean sameRead = false; + boolean sameUpdate = false; + boolean sameDelete = false; + // look at all 4 CRUD values and their Parent Perms and if they are the same as the CRUD value + Permission parentPerm = getFirstParentPermission(changedElement, Crud.Type.CREATE); + if (parentPerm != null) { if( (perm.getCRUDValue(Crud.Type.CREATE) == parentPerm.getCRUDValue(Crud.Type.CREATE)) || (perm.getCRUDValue(Crud.Type.CREATE) == null && parentPerm.getCRUDValue(Crud.Type.CREATE) != null) ) { - sameCreate = true; - } - } - if( !sameCreate ) return; - - parentPerm = getFirstParentPermission(changedElement, Crud.Type.READ); - if( parentPerm != null ) { + sameCreate = true; + } + } + if (!sameCreate) return; + + parentPerm = getFirstParentPermission(changedElement, Crud.Type.READ); + if (parentPerm != null) { if( (perm.getCRUDValue(Crud.Type.READ) == parentPerm.getCRUDValue(Crud.Type.READ)) || (perm.getCRUDValue(Crud.Type.READ) == null && parentPerm.getCRUDValue(Crud.Type.READ) != null) ) { - sameRead = true; - } - } - if( !sameRead ) return; - - parentPerm = getFirstParentPermission(changedElement, Crud.Type.UPDATE); - if( parentPerm != null ) { + sameRead = true; + } + } + if (!sameRead) return; + + parentPerm = getFirstParentPermission(changedElement, Crud.Type.UPDATE); + if (parentPerm != null) { if( (perm.getCRUDValue(Crud.Type.UPDATE) == parentPerm.getCRUDValue(Crud.Type.UPDATE)) || (perm.getCRUDValue(Crud.Type.UPDATE) == null && parentPerm.getCRUDValue(Crud.Type.UPDATE) != null) ) { - sameUpdate = true; - } - } - if( !sameUpdate ) return; - - parentPerm = getFirstParentPermission(changedElement, Crud.Type.DELETE); - if( parentPerm != null ) { + sameUpdate = true; + } + } + if (!sameUpdate) return; + + parentPerm = getFirstParentPermission(changedElement, Crud.Type.DELETE); + if (parentPerm != null) { if( (perm.getCRUDValue(Crud.Type.DELETE) == parentPerm.getCRUDValue(Crud.Type.DELETE)) || (perm.getCRUDValue(Crud.Type.DELETE) == null && parentPerm.getCRUDValue(Crud.Type.DELETE) != null) ) { - sameDelete = true; - } - } - - if( sameCreate && sameRead && sameUpdate && sameDelete ) { - //System.out.println(" Removing Stale Permission for: " + perm.getTargetName()); - permissionsMap.remove(changedElement); - //stalePermissionKeys.add(changedElement); - } - } - } - - /* - * Gather up all child and grand-child permissions - */ + sameDelete = true; + } + } + + if (sameCreate && sameRead && sameUpdate && sameDelete) { + // System.out.println(" Removing Stale Permission for: " + perm.getTargetName()); + permissionsMap.remove(changedElement); + // stalePermissionKeys.add(changedElement); + } + } + } + + /* + * Gather up all child and grand-child permissions + */ private void getChildPermissions(Object parent, Collection allChildPermissions) { - for( Object child : tree.getChildren(parent) ) { - Permission perm = this.permissionsMap.get(child); - if( perm != null ) { - allChildPermissions.add(perm); - } - getChildPermissions(child, allChildPermissions); - } - } - - /* - * Gather all children below the parent that have an associated permission. - * - */ + for (Object child : tree.getChildren(parent)) { + Permission perm = this.permissionsMap.get(child); + if (perm != null) { + allChildPermissions.add(perm); + } + getChildPermissions(child, allChildPermissions); + } + } + + /* + * Gather all children below the parent that have an associated permission. + * + */ private void getChildrenWithPermission(Object parent, Collection childrenWithPermission) { - for( Object child : tree.getChildren(parent) ) { - Permission perm = this.permissionsMap.get(child); - if( perm != null ) { - childrenWithPermission.add(child); - } - getChildrenWithPermission(child, childrenWithPermission); - } - } - - /** - * Finds the first parent permission which contains a non-null Boolean crud value - * @param element - * @param crudType - * @return - */ + for (Object child : tree.getChildren(parent)) { + Permission perm = this.permissionsMap.get(child); + if (perm != null) { + childrenWithPermission.add(child); + } + getChildrenWithPermission(child, childrenWithPermission); + } + } + + /** + * Finds the first parent permission which contains a non-null Boolean crud value + * @param element + * @param crudType + * @return + */ public Permission getExistingPermission(Object element, Crud.Type crudType) { - Permission perm = this.permissionsMap.get(element); - if( perm == null || perm.getCRUDValue(crudType) == null ) { - Object parent = tree.getParent(element); - while( parent != null && (perm == null || perm.getCRUDValue(crudType) == null) ) { - perm = this.permissionsMap.get(parent); - parent = tree.getParent(parent); - } - } - - return perm; - } - - /* - * Find the first parent above the element with an NON-NULL boolean value for the specified CRUD type - * - */ + Permission perm = this.permissionsMap.get(element); + if (perm == null || perm.getCRUDValue(crudType) == null) { + Object parent = tree.getParent(element); + while (parent != null && (perm == null || perm.getCRUDValue(crudType) == null)) { + perm = this.permissionsMap.get(parent); + parent = tree.getParent(parent); + } + } + + return perm; + } + + /* + * Find the first parent above the element with an NON-NULL boolean value for the specified CRUD type + * + */ private Permission getFirstParentPermission(Object element, Crud.Type crudType) { - Permission perm = null; - Object parent = tree.getParent(element); - while( parent != null && (perm == null || perm.getCRUDValue(crudType) == null) ) { - perm = this.permissionsMap.get(parent); - parent = tree.getParent(parent); - } - return perm; - } - - public Permission getPermission(Object element) { - return this.permissionsMap.get(element); - } - - /** - * Helper method to determine if any child below a parent contains a CRUD value different than the parent. + Permission perm = null; + Object parent = tree.getParent(element); + while (parent != null && (perm == null || perm.getCRUDValue(crudType) == null)) { + perm = this.permissionsMap.get(parent); + parent = tree.getParent(parent); + } + return perm; + } + + public Permission getPermission( Object element ) { + return this.permissionsMap.get(element); + } + + /** + * Helper method to determine if any child below a parent contains a CRUD value different than the parent. * - */ + */ public boolean hasChildWithDifferentCrudValue(Permission parentPermission, Object parent, Crud.Type type) { - Boolean parentValue = parentPermission.getCRUDValue(type); + Boolean parentValue = parentPermission.getCRUDValue(type); - Collection childPermissions = new ArrayList(); - - getChildPermissions(parent, childPermissions); - - for( Permission perm : childPermissions ) { - Boolean value = perm.getCRUDValue(type); - if( value != null && value != parentValue ) { - return true; - } - } - return false; - } - - public boolean hasPermissions() { - return !this.permissionsMap.isEmpty(); - } - + Collection childPermissions = new ArrayList(); + + getChildPermissions(parent, childPermissions); + + for (Permission perm : childPermissions) { + Boolean value = perm.getCRUDValue(type); + if (value != null && value != parentValue) { + return true; + } + } + return false; + } + + public boolean hasPermissions() { + return !this.permissionsMap.isEmpty(); + } + public void addPermission(Object key, Permission perm) { - if( !this.permissionsMap.containsKey(key)) { - this.permissionsMap.put(key, perm); - } - } - - /** + if (!this.permissionsMap.containsKey(key)) { + this.permissionsMap.put(key, perm); + } + } + + /** * Performs the necessary permission CRUDDELETE value changes based on the target element and the CRUD type. * This method is targeted for use by a single-click editor changing ONE CRUD boolean value for one object. - * - * @param element - * @param crudType - */ + * + * @param element + * @param crudType + */ public void togglePermission( Object element, Crud.Type crudType ) { - if( ! supportsUpdates(element, crudType) ) { - return; - } - Permission targetPermission = this.permissionsMap.get(element); + if (!supportsUpdates(element, crudType)) { + return; + } - if( targetPermission == null ) { - Permission parentPermission = getExistingPermission(element, crudType); - - // Now create New permission with NULL values - Crud targetCrud = new Crud(null, null, null, null); - targetPermission = new Permission(tree.getTargetName(element), targetCrud); - // Set the target permission crud value to parent permission crud value - // ONLY if the parent crud value == TRUE, else we can't override the parent. - if( parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { - targetPermission.setCRUDValue(parentPermission.getCRUDValue(crudType), crudType); - permissionsMap.put(element, targetPermission); - targetPermission.toggleCRUDValue(crudType); - } - } else { - // if the targetPermission's value == NULL, then we should treat this like a "new Permission" - if( targetPermission.getCRUDValue(crudType) == null ) { - Permission parentPermission = getExistingPermission(element, crudType); - // Set the target permission crud value to parent permission crud value - // ONLY if the parent crud value == TRUE, else we can't override the parent. - if( parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { - targetPermission.setCRUDValue(parentPermission.getCRUDValue(crudType), crudType); - targetPermission.toggleCRUDValue(crudType); - } - } else if( targetPermission.getCRUDValue(crudType) == Boolean.FALSE ) { - if( targetPermission.isPrimary() ) { - targetPermission.toggleCRUDValue(crudType); - } else { - Permission parentPermission = getFirstParentPermission(element, crudType); - // Set the target permission crud value to parent permission crud value - // ONLY if the parent crud value == TRUE, else we can't override the parent. - if( parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { - targetPermission.setCRUDValue(null, crudType); //parentPermission.getCRUDValue(type), type); - } - } - } else { - targetPermission.toggleCRUDValue(crudType); - } - - } - - // Now check if we need to remove any permissions based on the selected element. - cleanUpPermissions(element); - - // Check on all other permissions and clean them up if necessary - Collection childPermissions = new ArrayList(); - getChildrenWithPermission(element, childPermissions); - for( Object childWithPerm : childPermissions ) { - cleanUpPermissions(childWithPerm); - } - + // The current element permission + Permission targetPermission = this.permissionsMap.get(element); - } - - public boolean supportsUpdates(Object element, Crud.Type crudType) { - Object targetObj = element; - if( TransformationHelper.isSqlColumn(element) || element instanceof ProcedureParameter ) { - targetObj = ((EObject)element).eContainer(); - if( crudType == Type.DELETE ) { - return false; - } - } - - boolean isVirtualTable = TransformationHelper.isVirtualSqlTable(targetObj); + // Current element permission is null - inherits from parent + if (targetPermission == null) { + Permission parentPermission = getExistingPermission(element, crudType); - if (TransformationHelper.isSqlTable(targetObj) && !TransformationHelper.isXmlDocument(targetObj)) { + // Now create New permission with NULL values + Crud targetCrud = new Crud(null, null, null, null); + targetPermission = new Permission(tree.getTargetName(element), targetCrud); + + // Set the target permission crud value to parent permission crud value + // ONLY if the parent crud value == TRUE, else we can't override the parent. + if (parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { + targetPermission.setCRUDValue(parentPermission.getCRUDValue(crudType), crudType); + permissionsMap.put(element, targetPermission); + targetPermission.toggleCRUDValue(crudType); + } + // Current element permission non null - some portion already contains override. + } else { + // if the targetPermission's value == NULL, then we should treat this like a "new Permission" + if (targetPermission.getCRUDValue(crudType) == null) { + Permission parentPermission = getExistingPermission(element, crudType); + // Set the target permission crud value to parent permission crud value + // ONLY if the parent crud value == TRUE, else we can't override the parent. + if (parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { + targetPermission.setCRUDValue(parentPermission.getCRUDValue(crudType), crudType); + targetPermission.toggleCRUDValue(crudType); + } + } else if (targetPermission.getCRUDValue(crudType) == Boolean.FALSE) { + if (targetPermission.isPrimary()) { + targetPermission.toggleCRUDValue(crudType); + } else { + Permission parentPermission = getFirstParentPermission(element, crudType); + // Set the target permission crud value to parent permission crud value + // ONLY if the parent crud value == TRUE, else we can't override the parent. + if (parentPermission.getCRUDValue(crudType) == Boolean.TRUE) { + targetPermission.setCRUDValue(null, crudType); // parentPermission.getCRUDValue(type), type); + } + } + } else { + targetPermission.toggleCRUDValue(crudType); + } + + } + + // Now check if we need to remove any permissions based on the selected element. + cleanUpPermissions(element); + + // Check on all other permissions and clean them up if necessary + Collection childPermissions = new ArrayList(); + getChildrenWithPermission(element, childPermissions); + for (Object childWithPerm : childPermissions) { + cleanUpPermissions(childWithPerm); + } + + // If all of this node's siblings have same status - which is different than parent - change parent status, + // then do a clean-up + Object workingElem = element; + while (!isPrimary(workingElem) && allSiblingsHaveSameStatus(workingElem, crudType)) { + Object parent = tree.getParent(workingElem); + if (parent != null && !haveSameStatus(parent, workingElem, crudType)) { + // Get parent permission directly + Permission parentPerm = this.permissionsMap.get(parent); + Permission elementPerm = getExistingPermission(workingElem, crudType); + Boolean elementStatus = elementPerm.getCRUDValue(crudType); + // Paren permission is null means the crud was inherited, so add an override + if (parentPerm == null) { + Crud targetCrud = new Crud(null, null, null, null); + Permission newParentPermission = new Permission(tree.getTargetName(parent), targetCrud); + newParentPermission.setCRUDValue(elementStatus, crudType); + this.permissionsMap.put(parent, newParentPermission); + } else { + parentPerm.setCRUDValue(elementStatus, crudType); + } + // Now check if we need to remove any permissions based on the selected element. + cleanUpPermissions(parent); + + // Check on all other permissions and clean them up if necessary + Collection pchildPermissions = new ArrayList(); + getChildrenWithPermission(parent, pchildPermissions); + for (Object childWithPerm : pchildPermissions) { + cleanUpPermissions(childWithPerm); + } + } + workingElem = parent; + } + return; + } + + /** + * Check the 'toggle-ability' of the crud type of the supplied element. The status may return INFO - means a message should be + * displayed to the user before toggling WARNING - means the user should be shown a confirmation message before toggling OK - + * OK to toggle without showing a message or getting user confirmation + * + * @param element the element to toggle + * @param crudType the crud type to toggle + * @return the status of the toggle request + */ + public IStatus getToggleStatus( Object element, + Crud.Type crudType ) { + + // Return INFO - if element does not support updates + if (!supportsUpdates(element, crudType)) { + return createStatus(IStatus.INFO, RolesUiPlugin.UTIL.getString("PermissionHandler.infoNotUpdatable")); //$NON-NLS-1$ + } + + // Check for conditions where parent does not allow toggle + boolean parentAllowsToggle = true; + // The current element permission + Permission targetPermission = this.permissionsMap.get(element); + // Target Permission null, check existing permission from parent + if (targetPermission == null) { + Permission parentPermission = getExistingPermission(element, crudType); + if (parentPermission.getCRUDValue(crudType) != Boolean.TRUE) parentAllowsToggle = false; + } else { + if (targetPermission.getCRUDValue(crudType) == null) { + Permission parentPermission = getExistingPermission(element, crudType); + if (parentPermission.getCRUDValue(crudType) != Boolean.TRUE) parentAllowsToggle = false; + } else if (targetPermission.getCRUDValue(crudType) == Boolean.FALSE) { + if (!targetPermission.isPrimary()) { + Permission parentPermission = getFirstParentPermission(element, crudType); + if (parentPermission.getCRUDValue(crudType) != Boolean.TRUE) parentAllowsToggle = false; + } + } + } + // Return INFO - if parent state disallows toggle + if (!parentAllowsToggle) { + return createStatus(IStatus.INFO, RolesUiPlugin.UTIL.getString("PermissionHandler.infoParentNotEnabled")); //$NON-NLS-1$ + } + + // Return WARNING - if element can be toggled, but doing so will toggle all its children + Object[] children = tree.getChildren(element); + if (children.length > 0) { + String msg = null; + if (crudType == Crud.Type.READ) { + msg = RolesUiPlugin.UTIL.getString("PermissionHandler.warningWillToggleChildren"); //$NON-NLS-1$ + } else { + msg = RolesUiPlugin.UTIL.getString("PermissionHandler.warningWillToggleChildrenIfUpdatable"); //$NON-NLS-1$ + } + return createStatus(IStatus.WARNING, msg); + } + + // OK status - no message + return createStatus(IStatus.OK, ""); //$NON-NLS-1$ + } + + /** + * Create a Status object with the given status and message + * + * @param statusFlag the desired status + * @param msg the desired message + * @return the status object + */ + private IStatus createStatus( int statusFlag, + String msg ) { + final IStatus status = new Status(statusFlag, RolesUiPlugin.PLUGIN_ID, msg); + return status; + } + + /** + * Determine if all siblings of the supplied element have the same state for the specified crudType + * + * @param element the element to check + * @param crudType the crud type to check + * @return 'true' if all siblings have the same status + */ + private boolean allSiblingsHaveSameStatus( Object element, + Crud.Type crudType ) { + boolean allSame = true; + + // Get parent of the provided element + Object parent = tree.getParent(element); + // Compare status of all the children + if (parent != null) { + int i = 0; + Boolean firstStatus = null; + // Accumulate all of the sibling states + for (Object child : tree.getChildren(parent)) { + // Current child's permission. If it does not have a permission, it inherits the parents. + Permission perm = getExistingPermission(child, crudType); + Boolean currentStatus = null; + if (perm != null) { + currentStatus = perm.getCRUDValue(crudType); + } + if (i == 0) { + firstStatus = currentStatus; + } else { + if (currentStatus != firstStatus) { + allSame = false; + break; + } + } + i++; + } + } + return allSame; + } + + /** + * Determine if element 'isPrimary' + */ + private boolean isPrimary( Object element ) { + Permission perm = permissionsMap.get(element); + if (perm != null && perm.isPrimary()) { + return true; + } + return false; + } + + /** + * Helper method - determine if the two supplied elements have the same status for the specified crud type. + */ + private boolean haveSameStatus( Object elem1, + Object elem2, + Crud.Type crudType ) { + Permission elem1Perm = getExistingPermission(elem1, crudType); + Permission elem2Perm = getExistingPermission(elem2, crudType); + + Boolean elem1Status = elem1Perm.getCRUDValue(crudType); + Boolean elem2Status = elem2Perm.getCRUDValue(crudType); + + if (elem1Status != null && elem2Status != null && elem1Status == elem2Status) { + return true; + } + return false; + } + + public boolean supportsUpdates( Object element, + Crud.Type crudType ) { + Object targetObj = element; + if (TransformationHelper.isSqlColumn(element) || element instanceof ProcedureParameter) { + targetObj = ((EObject)element).eContainer(); + if (crudType == Type.DELETE) { + return false; + } + } + + boolean isVirtualTable = TransformationHelper.isVirtualSqlTable(targetObj); + + if (TransformationHelper.isSqlTable(targetObj) && !TransformationHelper.isXmlDocument(targetObj)) { SqlTableAspect tableAspect = (SqlTableAspect)com.metamatrix.modeler.core.metamodel.aspect.sql.SqlAspectHelper.getSqlAspect((EObject)targetObj); if (tableAspect != null) { - if( isVirtualTable ) { - EObject transMappingRoot = TransformationHelper.getTransformationMappingRoot((EObject)targetObj); - switch( crudType ) { + if (isVirtualTable) { + EObject transMappingRoot = TransformationHelper.getTransformationMappingRoot((EObject)targetObj); + switch (crudType) { case CREATE: return TransformationHelper.isInsertAllowed(transMappingRoot); case READ: return true; case UPDATE: return TransformationHelper.isUpdateAllowed(transMappingRoot); case DELETE: return TransformationHelper.isDeleteAllowed(transMappingRoot); - } + } } else { - return tableAspect.supportsUpdate((EObject)targetObj); + return tableAspect.supportsUpdate((EObject)targetObj); } } } else { - return true; + return true; } - - return false; - } + return false; + } }