@Override
 public boolean isValidForDataViewer(Connection connection, IMetadataTable metadataTable) {
   if (!canHandle(connection)) {
     return false;
   }
   if (metadataTable == null) {
     return false;
   }
   String partition = metadataTable.getAdditionalProperties().get(HCatalogConstants.PARTITIONS);
   String partitionName = ExtractMetaDataFromHCatalog.extractPartitionNameByJsonStr(partition);
   if (StringUtils.isEmpty(partitionName)) {
     Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
     MessageDialog.openWarning(
         shell,
         Messages.getString("HCatalogDragAndDropHandler.dataViewer.warning.title"), // $NON-NLS-1$
         Messages.getString("HCatalogDragAndDropHandler.dataViewer.warning.msg")); // $NON-NLS-1$
     return false;
   }
   return true;
 }
/** DOC ycbai class global comment. Detailled comment */
public class CreateHCatalogSchemaAction extends AbstractCreateAction {

  private static final String CREATE_LABEL =
      Messages.getString("CreateHCatalogSchemaAction.createLabel"); // $NON-NLS-1$

  private static final String EDIT_LABEL =
      Messages.getString("CreateHCatalogSchemaAction.editLabel"); // $NON-NLS-1$

  protected static final int WIZARD_WIDTH = 800;

  protected static final int WIZARD_HEIGHT = 500;

  public CreateHCatalogSchemaAction() {
    super();
    setText(CREATE_LABEL);
    setToolTipText(CREATE_LABEL);
    setImageDescriptor(ImageProvider.getImageDesc(ECoreImage.METADATA_TABLE_ICON));
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * org.talend.repository.ui.actions.metadata.AbstractCreateAction#init(org.talend.repository.model.RepositoryNode)
   */
  @Override
  protected void init(RepositoryNode node) {
    ERepositoryObjectType nodeType =
        (ERepositoryObjectType) node.getProperties(EProperties.CONTENT_TYPE);
    IProxyRepositoryFactory factory = ProxyRepositoryFactory.getInstance();
    if (factory.isUserReadOnlyOnCurrentProject()
        || !ProjectManager.getInstance().isInCurrentMainProject(node)) {
      setEnabled(false);
    } else {
      if (ENodeType.REPOSITORY_ELEMENT.equals(node.getType())) {
        if (node.getObject().getRepositoryStatus() == ERepositoryStatus.DELETED
            || node.getObject().getRepositoryStatus() == ERepositoryStatus.LOCK_BY_OTHER) {
          setEnabled(false);
          return;
        }
        if ((factory.getStatus(node.getObject()) == ERepositoryStatus.DELETED)
            || (factory.getStatus(node.getObject()) == ERepositoryStatus.LOCK_BY_OTHER)) {
          setEnabled(false);
          return;
        }

        if (ERepositoryObjectType.METADATA_CON_TABLE.equals(nodeType)
            || ERepositoryObjectType.METADATA_CON_COLUMN.equals(nodeType)) {
          RepositoryNode parent = node.getParent();
          if (parent != null
              && HCatalogRepositoryNodeType.HCATALOG.equals(
                  parent.getProperties(EProperties.CONTENT_TYPE))) {
            setText(EDIT_LABEL);
            collectSiblingNames(node);
            setEnabled(true);
            return;
          }
        } else if (HCatalogRepositoryNodeType.HCATALOG.equals(nodeType)) {
          setText(CREATE_LABEL);
          collectChildNames(node);
          setEnabled(true);
          return;
        }
      }
    }
  }

  @Override
  public Class getClassForDoubleClick() {
    return HCatalogConnection.class;
  }

  @Override
  protected void doRun() {
    if (repositoryNode == null) {
      repositoryNode = getCurrentRepositoryNode();
    }
    HCatalogConnection connection = null;
    MetadataTable metadataTable = null;
    boolean creation = false;
    if (repositoryNode.getType() == ENodeType.REPOSITORY_ELEMENT) {
      ERepositoryObjectType nodeType =
          (ERepositoryObjectType) repositoryNode.getProperties(EProperties.CONTENT_TYPE);
      String metadataTableLabel = (String) repositoryNode.getProperties(EProperties.LABEL);

      HCatalogConnectionItem item = null;
      if (nodeType == ERepositoryObjectType.METADATA_CON_TABLE) {
        item = (HCatalogConnectionItem) repositoryNode.getObject().getProperty().getItem();
        connection = (HCatalogConnection) item.getConnection();
        metadataTable = TableHelper.findByLabel(connection, metadataTableLabel);
        creation = false;
      } else if (nodeType == HCatalogRepositoryNodeType.HCATALOG) {
        item = (HCatalogConnectionItem) repositoryNode.getObject().getProperty().getItem();
        connection = (HCatalogConnection) item.getConnection();
        creation = true;
      } else {
        return;
      }

      boolean isOK = true;
      if (creation) {
        isOK = checkHCatalogConnection((HCatalogConnection) item.getConnection());
      }
      if (isOK) {
        openHCatalogSchemaWizard(item, metadataTable, false, creation);
      }
    }
  }

  private boolean checkHCatalogConnection(final HCatalogConnection connection) {
    final boolean[] result = new boolean[] {true};
    IRunnableWithProgress runnableWithProgress =
        new IRunnableWithProgress() {

          @Override
          public void run(IProgressMonitor monitor)
              throws InvocationTargetException, InterruptedException {
            monitor.beginTask(
                Messages.getString("CreateHCatalogSchemaAction.checkConnection"),
                IProgressMonitor.UNKNOWN);
            try {
              final ConnectionStatus connectionStatus =
                  HCatalogServiceUtil.testConnection(connection);
              if (!connectionStatus.getResult()) {
                PlatformUI.getWorkbench()
                    .getDisplay()
                    .syncExec(
                        new Runnable() {

                          @Override
                          public void run() {
                            new ErrorDialogWidthDetailArea(
                                PlatformUI.getWorkbench().getDisplay().getActiveShell(),
                                Activator.PLUGIN_ID,
                                Messages.getString(
                                    "CreateHCatalogSchemaAction.connectionFailure.mainMsg"),
                                connectionStatus //$NON-NLS-1$
                                    .getMessageException());
                            result[0] = false;
                            return;
                          }
                        });
              }

            } catch (Exception e) {
            } finally {
              monitor.done();
            }
          }
        };
    ProgressMonitorDialog dialog = new ProgressMonitorDialog(Display.getDefault().getActiveShell());
    try {
      dialog.run(true, true, runnableWithProgress);
    } catch (Exception e) {
      result[0] = false;
      ExceptionHandler.process(e);
    }

    return result[0];
  }

  private void openHCatalogSchemaWizard(
      final HCatalogConnectionItem item,
      final MetadataTable metadataTable,
      final boolean forceReadOnly,
      final boolean creation) {
    WizardDialog wizardDialog =
        new WizardDialog(
            Display.getDefault().getActiveShell(),
            new HCatalogSchemaWizard(
                PlatformUI.getWorkbench(),
                creation,
                repositoryNode.getObject(),
                metadataTable,
                getExistingNames(),
                forceReadOnly));
    wizardDialog.setPageSize(WIZARD_WIDTH, WIZARD_HEIGHT);
    wizardDialog.create();
    wizardDialog.open();
  }
}