Search in sources :

Example 61 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class OracleJdbcBlobFieldDefinition method setPreparedStatementValue.

@Override
public int setPreparedStatementValue(final PreparedStatement statement, final int parameterIndex, final Object value) throws SQLException {
    if (value == null) {
        final int sqlType = getSqlType();
        statement.setNull(parameterIndex, sqlType);
    } else {
        if (value instanceof Blob) {
            final Blob blob = (Blob) value;
            statement.setBlob(parameterIndex, blob);
        } else {
            InputStream in;
            if (value instanceof Blob) {
                final Blob blob = (Blob) value;
                in = blob.getBinaryStream();
            } else if (value instanceof byte[]) {
                final byte[] bytes = (byte[]) value;
                in = new ByteArrayInputStream(bytes);
            } else if (value instanceof CharSequence) {
                final String string = ((CharSequence) value).toString();
                final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
                in = new ByteArrayInputStream(bytes);
            } else {
                try {
                    final Resource resource = Resource.getResource(value);
                    in = resource.newBufferedInputStream();
                } catch (final IllegalArgumentException e) {
                    throw new IllegalArgumentException(value.getClass() + " not valid for a blob column");
                }
            }
            statement.setBinaryStream(parameterIndex, in);
        }
    }
    return parameterIndex + 1;
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(com.revolsys.spring.resource.Resource)

Example 62 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class PostgreSQLJdbcBlobFieldDefinition method setPreparedStatementValue.

@Override
public int setPreparedStatementValue(final PreparedStatement statement, final int parameterIndex, final Object value) throws SQLException {
    if (value == null) {
        final int sqlType = getSqlType();
        statement.setNull(parameterIndex, sqlType);
    } else {
        Blob blob;
        if (value instanceof Blob) {
            blob = (Blob) value;
            statement.setBlob(parameterIndex, blob);
        } else {
            InputStream in;
            if (value instanceof InputStream) {
                in = (InputStream) value;
            } else if (value instanceof byte[]) {
                final byte[] bytes = (byte[]) value;
                in = new ByteArrayInputStream(bytes);
            } else if (value instanceof CharSequence) {
                final String string = ((CharSequence) value).toString();
                final byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
                in = new ByteArrayInputStream(bytes);
            } else {
                try {
                    final Resource resource = Resource.getResource(value);
                    in = resource.newBufferedInputStream();
                } catch (final IllegalArgumentException e) {
                    throw new IllegalArgumentException(value.getClass() + " not valid for a blob column");
                }
            }
            try {
                final PGConnection pgConnection = (PGConnection) ((DelegatingConnection<?>) statement.getConnection()).getInnermostDelegate();
                final LargeObjectManager lobManager = pgConnection.getLargeObjectAPI();
                final long lobId = lobManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE);
                final LargeObject lob = lobManager.open(lobId, LargeObjectManager.WRITE);
                try {
                    final byte[] buffer = new byte[2048];
                    int readCount = 0;
                    while ((readCount = in.read(buffer, 0, 2048)) > 0) {
                        lob.write(buffer, 0, readCount);
                    }
                } finally {
                    lob.close();
                }
                statement.setLong(parameterIndex, lobId);
            } catch (final IOException e) {
                Exceptions.throwUncheckedException(e);
            }
        }
    }
    return parameterIndex + 1;
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Resource(com.revolsys.spring.resource.Resource) IOException(java.io.IOException) PGConnection(org.postgresql.PGConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) LargeObjectManager(org.postgresql.largeobject.LargeObjectManager) LargeObject(org.postgresql.largeobject.LargeObject)

Example 63 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class AbstractGeoreferencedImage method saveChanges.

@Override
public boolean saveChanges() {
    try {
        final Resource resource = this.imageResource;
        final Resource rgResource = resource.newResourceAddExtension("rgobject");
        MapObjectFactory.write(rgResource, this);
        setHasChanges(false);
        return true;
    } catch (final Throwable e) {
        Logs.error(this, "Unable to save: " + this.imageResource + ".rgobject", e);
        return false;
    }
}
Also used : Resource(com.revolsys.spring.resource.Resource)

Example 64 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class AbstractGeoreferencedImage method loadSettings.

protected long loadSettings() {
    final Resource resource = getImageResource();
    final Resource settingsFile = resource.newResourceAddExtension("rgobject");
    if (settingsFile.exists()) {
        try {
            Map<String, Object> settings;
            try {
                settings = Json.toMap(settingsFile);
            } catch (final Throwable e) {
                settings = new LinkedHashMapEx();
            }
            final String boundingBoxWkt = (String) settings.get("boundingBox");
            if (Property.hasValue(boundingBoxWkt)) {
                final BoundingBox boundingBox = BoundingBox.newBoundingBox(boundingBoxWkt);
                if (!boundingBox.isEmpty()) {
                    setBoundingBox(boundingBox);
                }
            }
            final List<?> tiePointsProperty = (List<?>) settings.get("tiePoints");
            final List<MappedLocation> tiePoints = new ArrayList<>();
            if (tiePointsProperty != null) {
                for (final Object tiePointValue : tiePointsProperty) {
                    if (tiePointValue instanceof MappedLocation) {
                        tiePoints.add((MappedLocation) tiePointValue);
                    } else if (tiePointValue instanceof Map) {
                        @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) tiePointValue;
                        tiePoints.add(new MappedLocation(map));
                    }
                }
            }
            if (!tiePoints.isEmpty()) {
                setTiePoints(tiePoints);
            }
            return settingsFile.getLastModified();
        } catch (final Throwable e) {
            Logs.error(this, "Unable to load:" + settingsFile, e);
            return -1;
        }
    } else {
        return -1;
    }
}
Also used : Resource(com.revolsys.spring.resource.Resource) ArrayList(java.util.ArrayList) PropertyChangeArrayList(com.revolsys.collection.PropertyChangeArrayList) BoundingBox(com.revolsys.geometry.model.BoundingBox) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) PropertyChangeArrayList(com.revolsys.collection.PropertyChangeArrayList) List(java.util.List) LinkedHashMapEx(com.revolsys.collection.map.LinkedHashMapEx) HashMap(java.util.HashMap) Map(java.util.Map)

Example 65 with Resource

use of com.revolsys.spring.resource.Resource in project com.revolsys.open by revolsys.

the class AbstractGeoreferencedImage method loadWorldFile.

protected void loadWorldFile() {
    final Resource resource = getImageResource();
    final Resource worldFile = resource.newResourceChangeExtension(getWorldFileExtension());
    loadWorldFile(worldFile);
}
Also used : Resource(com.revolsys.spring.resource.Resource)

Aggregations

Resource (com.revolsys.spring.resource.Resource)78 PathResource (com.revolsys.spring.resource.PathResource)23 MapEx (com.revolsys.collection.map.MapEx)9 File (java.io.File)9 IOException (java.io.IOException)8 InputStream (java.io.InputStream)6 LinkedHashMapEx (com.revolsys.collection.map.LinkedHashMapEx)5 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)5 UrlResource (com.revolsys.spring.resource.UrlResource)5 Writer (java.io.Writer)5 Record (com.revolsys.record.Record)4 RecordDefinition (com.revolsys.record.schema.RecordDefinition)4 DataType (com.revolsys.datatype.DataType)3 BoundingBox (com.revolsys.geometry.model.BoundingBox)3 Geometry (com.revolsys.geometry.model.Geometry)3 AbstractRecordWriter (com.revolsys.io.AbstractRecordWriter)3 ArrayRecord (com.revolsys.record.ArrayRecord)3 RecordReader (com.revolsys.record.io.RecordReader)3 RecordWriter (com.revolsys.record.io.RecordWriter)3 Blob (java.sql.Blob)3