Skip to content

Commit

Permalink
Merge pull request #317 from mdrillin/TEIIDDES-2087
Browse files Browse the repository at this point in the history
TEIIDDES-2087 Add property validation for Google Source
  • Loading branch information
blafond committed Apr 1, 2014
2 parents 5741c4c + dcaf6c9 commit 0959cf3
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public final class DataSourcePropertiesPanel extends Composite implements UiCons
private List<PropertyItem> propertyItemList = new ArrayList<PropertyItem>();
private boolean isCreateNew = false;
private boolean isReadOnly = false;

private PropertyLabelProvider namePropLabelProvider;
private PropertyLabelProvider valuePropLabelProvider;

private List<DataSourcePropertiesPanelListener> listeners = new ArrayList<DataSourcePropertiesPanelListener>();
private Button resetButton;

Expand Down Expand Up @@ -184,12 +186,14 @@ public void inputChanged( Viewer viewer,
// create columns
TableViewerColumn column = new TableViewerColumn(this.propertiesViewer, SWT.LEFT);
column.getColumn().setText(Messages.dataSourcePropertiesPanel_nameColText);
column.setLabelProvider(new PropertyLabelProvider(true));
namePropLabelProvider = new PropertyLabelProvider(true,this.propertyItemList);
column.setLabelProvider(namePropLabelProvider);
column.getColumn().pack();

column = new TableViewerColumn(this.propertiesViewer, SWT.LEFT);
column.getColumn().setText(Messages.dataSourcePropertiesPanel_valueColText);
column.setLabelProvider(new PropertyLabelProvider(false));
valuePropLabelProvider = new PropertyLabelProvider(false,this.propertyItemList);
column.setLabelProvider(valuePropLabelProvider);
// Add editing support if its not readonly
if(!isReadOnly) {
column.setEditingSupport(new DataSourcePropertyEditingSupport(this.propertiesViewer,this));
Expand Down Expand Up @@ -250,7 +254,7 @@ private void handleResetProperty() {
assert (!this.propertiesViewer.getSelection().isEmpty());
PropertyItem prop = getSelectedProperty();
prop.reset();
this.propertiesViewer.refresh(prop);
this.propertiesViewer.refresh();
this.resetButton.setEnabled(false);
}

Expand Down Expand Up @@ -297,14 +301,50 @@ public Properties getDataSourceProperties() {
String propValue = propObj.getValue();
String defaultValue = propObj.getDefaultValue();
boolean isModifiable = propObj.isModifiable();
boolean isRequired = propObj.isRequired();
boolean isRequired = isPropertyRequired(propObj,this.propertyItemList);
if(isRequired || (isModifiable && !valuesSame(propValue,defaultValue))) {
resultProperties.setProperty(propName,propValue);
}
}
return resultProperties;
}

/*
* Do extra source-specific checks for inter-dependent properties (e.g. Google)
* @param propItem the property item
* @param propItems the list of items
* @return 'true' if really required, 'false' if not
*/
private boolean isPropertyRequired(PropertyItem propItem, List<PropertyItem> propItems) {
boolean isRequired = propItem.isRequired();

// Further checking for inter-dependent props
if(isRequired) {
// Special handling for Google. Some properties are required 'conditionally'
if(isGoogleSource(propItems)) {
String propName = propItem.getName();
if(propName!=null) {
// RefreshToken is not required if AuthMethod = 'ClientLogin'
if(propName.equalsIgnoreCase(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_KEY_REFRESH_TOKEN)) {
String authValue = getPropertyValue(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_KEY_AUTH_METHOD,this.propertyItemList);
if(authValue!=null && authValue.equalsIgnoreCase(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_VALUE_AUTH_CLIENT_LOGIN)) {
isRequired = false;
}
// Username and Password are not required if AuthMethod = "OAuth2"
} else if(propName.equalsIgnoreCase(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_KEY_USERNAME) ||
propName.equalsIgnoreCase(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_KEY_PASSWORD)) {
String authValue = getPropertyValue(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_KEY_AUTH_METHOD,this.propertyItemList);
if(authValue!=null && authValue.equalsIgnoreCase(TranslatorHelper.GOOGLE_SOURCE_PROPERTY_VALUE_AUTH_OAUTH2)) {
isRequired = false;
}
}
}
}
}

return isRequired;
}

private boolean valuesSame(String value1, String value2) {
if(CoreStringUtil.isEmpty(value1) && CoreStringUtil.isEmpty(value2)) {
return true;
Expand Down Expand Up @@ -346,6 +386,8 @@ private void updatePropertiesList( ) {
} else {
this.propertyItemList = sortPropertyItems(this.dataSourceMgr.getDataSourcePropertyItems(this.dataSourceOrDriverName));
}
this.namePropLabelProvider.setPropertyItems(this.propertyItemList);
this.valuePropLabelProvider.setPropertyItems(this.propertyItemList);
}
}

Expand Down Expand Up @@ -478,7 +520,7 @@ void handleRestorePropertyDefaultValue() {

PropertyItem prop = getSelectedProperty();
prop.setValue(null);
this.propertiesViewer.refresh(prop);
this.propertiesViewer.refresh();
this.resetButton.setEnabled(false);
}

Expand All @@ -487,13 +529,44 @@ void handleRestorePropertyDefaultValue() {
* @return the current Status
*/
public IStatus getStatus() {
IStatus resultStatus = new Status(IStatus.OK, PLUGIN_ID, Messages.dataSourcePropertiesPanelOk);

for(PropertyItem propObj : this.propertyItemList) {
if(!propObj.hasValidValue()) {
return new Status(IStatus.ERROR, PLUGIN_ID, Messages.dataSourcePropertiesPanel_invalidPropertyMsg);
if(isPropertyRequired(propObj,this.propertyItemList) && !propObj.hasValidValue()) {
resultStatus = new Status(IStatus.ERROR, PLUGIN_ID, Messages.dataSourcePropertiesPanel_invalidPropertyMsg);
break;
}
}

return new Status(IStatus.OK, PLUGIN_ID, Messages.dataSourcePropertiesPanelOk);

return resultStatus;
}

private boolean isGoogleSource(List<PropertyItem> propertyItems) {
boolean isGoogle = false;
String classNameValue = getPropertyValue("class-name", propertyItems); //$NON-NLS-1$
if(classNameValue!=null && classNameValue.equalsIgnoreCase(TranslatorHelper.TEIID_GOOGLE_CLASS)) {
isGoogle = true;
}
return isGoogle;
}

/**
* Gets the property value from the propertyItems for the supplied key. If a property matching the supplied key is not found,
* returns null
* @param propKey the property key
* @param propertyItems the list of property items
* @return the property value for the supplied key
*/
private String getPropertyValue(String propKey, List<PropertyItem> propertyItems) {
String propValue = null;
for(PropertyItem propItem : propertyItems) {
String propName = propItem.getName();
if(propName!=null && propName.equalsIgnoreCase(propKey)) {
propValue = propItem.getValue();
break;
}
}
return propValue;
}

/**
Expand All @@ -511,9 +584,15 @@ public void refresh() {
class PropertyLabelProvider extends ColumnLabelProvider {

private final boolean nameColumn;
private List<PropertyItem> propertyItems;

public PropertyLabelProvider( boolean nameColumn ) {
public PropertyLabelProvider( boolean nameColumn, List<PropertyItem> propertyItems ) {
this.nameColumn = nameColumn;
this.propertyItems = propertyItems;
}

public void setPropertyItems(List<PropertyItem> propItems) {
this.propertyItems = propItems;
}

/**
Expand All @@ -538,8 +617,8 @@ public Image getImage( Object element ) {

// determine if valid property
boolean hasValidValue = property.hasValidValue();

if (!hasValidValue) {
boolean isPropRqd = isPropertyRequired(property,this.propertyItems);
if (isPropRqd && !hasValidValue) {
image = errorImage;
}
}
Expand All @@ -557,7 +636,7 @@ public String getText( Object element ) {
PropertyItem property = (PropertyItem)element;

if (this.nameColumn) {
if(property.isRequired()) {
if(isPropertyRequired(property,this.propertyItems)) {
return "* "+property.getDisplayName(); //$NON-NLS-1$
}
return property.getDisplayName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected void setValue( Object element,
}

setElementValue(element, newValue);
getViewer().refresh(element);
getViewer().refresh();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public boolean hasValidValue() {
boolean isValid = true;

// If its required, must have a value
if(this.isRequired && CoreStringUtil.isEmpty(this.value)) {
if(CoreStringUtil.isEmpty(this.value)) {
isValid = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public class TranslatorHelper implements UiConstants {
public static final String VENDOR_KEY = "org.eclipse.datatools.connectivity.db.vendor"; //$NON-NLS-1$
public static final String VERSION_KEY = "org.eclipse.datatools.connectivity.db.version"; //$NON-NLS-1$


public static final String GOOGLE_SOURCE_PROPERTY_KEY_AUTH_METHOD = "AuthMethod"; //$NON-NLS-1$
public static final String GOOGLE_SOURCE_PROPERTY_VALUE_AUTH_CLIENT_LOGIN = "ClientLogin"; //$NON-NLS-1$
public static final String GOOGLE_SOURCE_PROPERTY_VALUE_AUTH_OAUTH2 = "OAuth2"; //$NON-NLS-1$
public static final String GOOGLE_SOURCE_PROPERTY_KEY_REFRESH_TOKEN = "RefreshToken"; //$NON-NLS-1$
public static final String GOOGLE_SOURCE_PROPERTY_KEY_USERNAME = "Username"; //$NON-NLS-1$
public static final String GOOGLE_SOURCE_PROPERTY_KEY_PASSWORD = "Password"; //$NON-NLS-1$

public static final String TEIID_FILE_DRIVER = "teiid-connector-file.rar"; //$NON-NLS-1$
public static final String TEIID_GOOGLE_DRIVER = "teiid-connector-google.rar"; //$NON-NLS-1$
public static final String TEIID_INFINISPAN_DRIVER = "teiid-connector-infinispan.rar"; //$NON-NLS-1$
Expand Down

0 comments on commit 0959cf3

Please sign in to comment.