Search in sources :

Example 16 with IHopMetadata

use of org.apache.hop.metadata.api.IHopMetadata in project hop by apache.

the class HopGuiAuditDelegate method openMetadataObject.

private void openMetadataObject(String className, String name) throws HopException {
    try {
        IHopMetadataProvider metadataProvider = hopGui.getMetadataProvider();
        MetadataPerspective perspective = MetadataPerspective.getInstance();
        // Find the class...
        List<Class<IHopMetadata>> metadataClasses = metadataProvider.getMetadataClasses();
        for (Class<IHopMetadata> metadataClass : metadataClasses) {
            if (metadataClass.getName().equals(className)) {
                // Get the serializer and open it up
                // 
                IHopMetadataSerializer<IHopMetadata> serializer = metadataProvider.getSerializer(metadataClass);
                // 
                if (serializer.exists(name)) {
                    IHopMetadata metadata = serializer.load(name);
                    MetadataManager<IHopMetadata> metadataManager = new MetadataManager<>(HopGui.getInstance().getVariables(), metadataProvider, metadataClass);
                    MetadataEditor<IHopMetadata> editor = metadataManager.createEditor(metadata);
                    // We assume that all tab items are closed so we can just open up a new editor for the
                    // metadata
                    // 
                    perspective.addEditor(editor);
                }
            }
        }
    } catch (Exception e) {
        throw new HopException("Error opening metadata object '" + name + "' of class " + className, e);
    }
}
Also used : MetadataManager(org.apache.hop.ui.core.metadata.MetadataManager) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) HopException(org.apache.hop.core.exception.HopException) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) MetadataPerspective(org.apache.hop.ui.hopgui.perspective.metadata.MetadataPerspective) HopException(org.apache.hop.core.exception.HopException)

Example 17 with IHopMetadata

use of org.apache.hop.metadata.api.IHopMetadata in project hop by apache.

the class JsonMetadataProvider method getSerializer.

@Override
public <T extends IHopMetadata> IHopMetadataSerializer<T> getSerializer(Class<T> managedClass) throws HopException {
    if (managedClass == null) {
        throw new HopException("You need to specify the class to serialize");
    }
    // Is this a metadata class?
    // 
    HopMetadata hopMetadata = managedClass.getAnnotation(HopMetadata.class);
    if (hopMetadata == null) {
        throw new HopException("To serialize class " + managedClass.getClass().getName() + " it needs to have annotation " + HopMetadata.class.getName());
    }
    String classFolder = Const.NVL(hopMetadata.key(), hopMetadata.name());
    String serializerBaseFolderName = baseFolder + (baseFolder.endsWith(Const.FILE_SEPARATOR) ? "" : Const.FILE_SEPARATOR) + classFolder;
    // Check if the folder exists...
    // 
    FileObject serializerBaseFolder = HopVfs.getFileObject(serializerBaseFolderName);
    try {
        if (!serializerBaseFolder.exists()) {
            serializerBaseFolder.createFolder();
        }
    } catch (Exception e) {
        throw new HopException("Error validating or creating folder  '" + serializerBaseFolderName + "'to store JSON serialized objects in from class " + managedClass.getName());
    }
    return new JsonMetadataSerializer<>(this, serializerBaseFolderName, managedClass, variables, hopMetadata.name());
}
Also used : HopException(org.apache.hop.core.exception.HopException) HopMetadata(org.apache.hop.metadata.api.HopMetadata) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) FileObject(org.apache.commons.vfs2.FileObject) HopException(org.apache.hop.core.exception.HopException)

Example 18 with IHopMetadata

use of org.apache.hop.metadata.api.IHopMetadata in project hop by apache.

the class XmlMetadataUtil method serializeObjectToXml.

/**
 * This method looks at the fields in the class of the provided object. It then sees which fields
 * have annotation HopMetadataProperty and proceeds to serialize the values of those fields as
 * XML.
 *
 * @param object The object to serialize to XML
 * @return The XML representation of the given object.
 * @throws HopException
 */
public static String serializeObjectToXml(Object object) throws HopException {
    String xml = "";
    // Pick up all the @HopMetadataProperty annotations
    // Serialize them to XML
    // 
    List<Field> fields = new ArrayList(ReflectionUtil.findAllFields(object.getClass()));
    // Sort the fields by name to get stable XML as output
    // 
    Collections.sort(fields, Comparator.comparing(Field::getName));
    for (Field field : fields) {
        // 
        if (Modifier.isTransient(field.getModifiers()) || Modifier.isVolatile(field.getModifiers())) {
            continue;
        }
        HopMetadataProperty property = field.getAnnotation(HopMetadataProperty.class);
        if (property != null) {
            String groupKey = property.groupKey();
            String tag = property.key();
            if (StringUtils.isEmpty(tag)) {
                tag = field.getName();
            }
            Class<?> fieldType = field.getType();
            // Is this a boolean?
            // 
            boolean isBoolean = Boolean.class.equals(fieldType) || boolean.class.equals(fieldType);
            // A password?
            // 
            boolean isPassword = property.password();
            // Store enums with their code?
            // 
            boolean storeWithCode = property.storeWithCode();
            // Get the value of the field...
            // 
            Object value = ReflectionUtil.getFieldValue(object, field.getName(), isBoolean);
            if (value != null) {
                // 
                if (property.storeWithName()) {
                    xml += XmlHandler.addTagValue(tag, ((IHopMetadata) value).getName());
                } else {
                    xml += serializeObjectToXml(value, groupKey, tag, isPassword, storeWithCode);
                }
            }
        }
    }
    return xml;
}
Also used : HopMetadataProperty(org.apache.hop.metadata.api.HopMetadataProperty) Field(java.lang.reflect.Field) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata)

Example 19 with IHopMetadata

use of org.apache.hop.metadata.api.IHopMetadata in project hop by apache.

the class HopMetadataUtil method getHopMetadataKeys.

public static String[] getHopMetadataKeys(IHopMetadataProvider provider) {
    List<String> keys = new ArrayList<>();
    for (Class<IHopMetadata> metadataClass : provider.getMetadataClasses()) {
        HopMetadata hopMetadata = getHopMetadataAnnotation(metadataClass);
        keys.add(hopMetadata.key());
    }
    Collections.sort(keys);
    return keys.toArray(new String[0]);
}
Also used : IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) ArrayList(java.util.ArrayList) HopMetadata(org.apache.hop.metadata.api.HopMetadata) IHopMetadata(org.apache.hop.metadata.api.IHopMetadata)

Example 20 with IHopMetadata

use of org.apache.hop.metadata.api.IHopMetadata in project hop by apache.

the class MetadataFileTypeHandler method findTabItemHandler.

private TabItemHandler findTabItemHandler() {
    if (metadata == null) {
        return null;
    }
    MetadataPerspective perspective = MetadataPerspective.getInstance();
    for (TabItemHandler tabItemHandler : perspective.getItems()) {
        CTabItem tabItem = tabItemHandler.getTabItem();
        MetadataEditor editor = (MetadataEditor) tabItem.getData();
        IHopMetadata other = editor.getMetadata();
        if (other.equals(metadata)) {
            return tabItemHandler;
        }
    }
    return null;
}
Also used : IHopMetadata(org.apache.hop.metadata.api.IHopMetadata) MetadataPerspective(org.apache.hop.ui.hopgui.perspective.metadata.MetadataPerspective) TabItemHandler(org.apache.hop.ui.hopgui.perspective.TabItemHandler) CTabItem(org.eclipse.swt.custom.CTabItem)

Aggregations

IHopMetadata (org.apache.hop.metadata.api.IHopMetadata)20 HopException (org.apache.hop.core.exception.HopException)10 ErrorDialog (org.apache.hop.ui.core.dialog.ErrorDialog)9 HopMetadata (org.apache.hop.metadata.api.HopMetadata)8 GuiToolbarElement (org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElement)6 IHopMetadataProvider (org.apache.hop.metadata.api.IHopMetadataProvider)6 MetadataManager (org.apache.hop.ui.core.metadata.MetadataManager)4 ArrayList (java.util.ArrayList)3 JsonMetadataParser (org.apache.hop.metadata.serializer.json.JsonMetadataParser)2 TabItemHandler (org.apache.hop.ui.hopgui.perspective.TabItemHandler)2 MetadataPerspective (org.apache.hop.ui.hopgui.perspective.metadata.MetadataPerspective)2 Image (org.eclipse.swt.graphics.Image)2 JSONObject (org.json.simple.JSONObject)2 Field (java.lang.reflect.Field)1 FileObject (org.apache.commons.vfs2.FileObject)1 GuiKeyboardShortcut (org.apache.hop.core.gui.plugin.key.GuiKeyboardShortcut)1 GuiOsxKeyboardShortcut (org.apache.hop.core.gui.plugin.key.GuiOsxKeyboardShortcut)1 IRowMeta (org.apache.hop.core.row.IRowMeta)1 RowMeta (org.apache.hop.core.row.RowMeta)1 AuditList (org.apache.hop.history.AuditList)1