ca.bc.gov.open.cpf.plugin.impl.PluginAdaptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cpf-api-plugin Show documentation
Show all versions of cpf-api-plugin Show documentation
Concurrent Processing Framework Business Application Plugin API
/*
* Copyright © 2008-2016, Province of British Columbia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ca.bc.gov.open.cpf.plugin.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import ca.bc.gov.open.cpf.plugin.api.log.AppLog;
import ca.bc.gov.open.cpf.plugin.api.security.SecurityService;
import com.revolsys.collection.map.Maps;
import com.revolsys.geometry.model.BoundingBox;
import com.revolsys.geometry.model.Geometry;
import com.revolsys.geometry.model.GeometryCollection;
import com.revolsys.geometry.model.GeometryFactory;
import com.revolsys.geometry.model.LineString;
import com.revolsys.geometry.model.Lineal;
import com.revolsys.geometry.model.Point;
import com.revolsys.geometry.model.Polygon;
import com.revolsys.geometry.model.Polygonal;
import com.revolsys.geometry.model.Punctual;
import com.revolsys.io.LazyHttpPostOutputStream;
import com.revolsys.parallel.ThreadUtil;
import com.revolsys.record.property.FieldProperties;
import com.revolsys.record.schema.FieldDefinition;
import com.revolsys.record.schema.RecordDefinition;
import com.revolsys.record.schema.RecordDefinitionImpl;
import com.revolsys.util.Booleans;
import com.revolsys.util.Exceptions;
import com.revolsys.util.MathUtil;
import com.revolsys.util.Property;
import com.revolsys.util.UrlUtil;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.impl.PackedCoordinateSequence;
public class PluginAdaptor {
public static final String[] INPUT_DATA_PARAMETER_NAMES = new String[] {
"inputDataUrl", "inputDataContentType"
};
public static final List INTERNAL_PROPERTY_NAMES = Arrays.asList("sequenceNumber",
"resultNumber");
public static Object getTestValue(final FieldDefinition field) {
Object value;
value = field.getDefaultValue();
if (value == null) {
final Class> typeClass = field.getTypeClass();
if (Boolean.class.isAssignableFrom(typeClass)) {
value = true;
} else if (Number.class.isAssignableFrom(typeClass)) {
value = 123;
} else if (URL.class.isAssignableFrom(typeClass)) {
String urlString = "http://www.test.com/";
final int length = field.getLength();
if (length > 0 && length < 4) {
urlString = urlString.substring(0, length);
}
value = UrlUtil.getUrl(urlString);
} else if (String.class.isAssignableFrom(typeClass)) {
value = "test";
final int length = field.getLength();
if (length > 0 && length < 4) {
value = ((String)value).substring(0, length);
}
} else if (Date.class.isAssignableFrom(typeClass)) {
final Timestamp time = new Timestamp(System.currentTimeMillis());
value = field.toFieldValue(time);
} else if (LineString.class.isAssignableFrom(typeClass)) {
value = GeometryFactory.wgs84().lineString(2, -125.0, 53.0, -125.1, 53.0);
} else if (Polygon.class.isAssignableFrom(typeClass)) {
final BoundingBox boundingBox = GeometryFactory.wgs84().newBoundingBox(-125.0, 53.0, -125.1,
53.0);
value = boundingBox.toPolygon(10);
} else if (Lineal.class.isAssignableFrom(typeClass)) {
final LineString line1 = GeometryFactory.wgs84().lineString(2, -125.0, 53.0, -125.1, 53.0);
final LineString line2 = GeometryFactory.wgs84().lineString(2, -125.2, 53.0, -125.3, 53.0);
value = GeometryFactory.wgs84().lineal(line1, line2);
} else if (Polygonal.class.isAssignableFrom(typeClass)) {
final BoundingBox boundingBox = GeometryFactory.wgs84().newBoundingBox(-125.0, 53.0, -125.1,
53.0);
final Polygon polygon1 = boundingBox.toPolygon(10);
final BoundingBox boundingBox2 = GeometryFactory.wgs84().newBoundingBox(-125.2, 53.0,
-125.3, 53.0);
final Polygon polygon2 = boundingBox2.toPolygon(10);
value = GeometryFactory.wgs84().polygonal(polygon1, polygon2);
} else if (Point.class.isAssignableFrom(typeClass)) {
value = GeometryFactory.wgs84().point(-125, 53);
} else if (GeometryCollection.class.isAssignableFrom(typeClass)
|| Punctual.class.isAssignableFrom(typeClass)) {
final Point point1 = GeometryFactory.wgs84().point(-125, 53);
final Point point2 = GeometryFactory.wgs84().point(-125, 53.1);
value = GeometryFactory.wgs84().punctual(point1, point2);
} else if (Geometry.class.isAssignableFrom(typeClass)) {
value = GeometryFactory.wgs84().point(-125, 53);
} else if (com.vividsolutions.jts.geom.Geometry.class.isAssignableFrom(typeClass)) {
// JTS
final com.vividsolutions.jts.geom.GeometryFactory jtsGeometryFactory = new com.vividsolutions.jts.geom.GeometryFactory(
new com.vividsolutions.jts.geom.PrecisionModel(
com.vividsolutions.jts.geom.PrecisionModel.FLOATING),
4326);
if (com.vividsolutions.jts.geom.LineString.class.isAssignableFrom(typeClass)) {
final PackedCoordinateSequence.Double points = new PackedCoordinateSequence.Double(
new double[] {
-125, 53, -125.1, 53
}, 2);
final com.vividsolutions.jts.geom.LineString line = jtsGeometryFactory
.createLineString(points);
value = line;
} else if (Polygon.class.isAssignableFrom(typeClass)) {
final PackedCoordinateSequence.Double points = new PackedCoordinateSequence.Double(
new double[] {
-125, 53, -125, 53.1, -125.1, 53.1, -125.1, 53, -125, 53
}, 2);
final com.vividsolutions.jts.geom.Polygon polygon = jtsGeometryFactory
.createPolygon(points);
value = polygon;
} else if (com.vividsolutions.jts.geom.MultiLineString.class.isAssignableFrom(typeClass)) {
final PackedCoordinateSequence.Double points = new PackedCoordinateSequence.Double(
new double[] {
-125, 53, -125.1, 53
}, 2);
final com.vividsolutions.jts.geom.LineString line = jtsGeometryFactory
.createLineString(points);
value = jtsGeometryFactory
.createMultiLineString(new com.vividsolutions.jts.geom.LineString[] {
line
});
} else if (com.vividsolutions.jts.geom.MultiPolygon.class.isAssignableFrom(typeClass)) {
final PackedCoordinateSequence.Double points = new PackedCoordinateSequence.Double(
new double[] {
-125, 53, -125, 53.1, -125.1, 53.1, -125.1, 53, -125, 53
}, 2);
final com.vividsolutions.jts.geom.Polygon polygon = jtsGeometryFactory
.createPolygon(points);
value = jtsGeometryFactory.createMultiPolygon(new com.vividsolutions.jts.geom.Polygon[] {
polygon
});
} else if (com.vividsolutions.jts.geom.GeometryCollection.class.isAssignableFrom(typeClass)
|| com.vividsolutions.jts.geom.MultiPoint.class.isAssignableFrom(typeClass)) {
final Coordinate[] coordinates = new Coordinate[] {
new Coordinate(-125, 53)
};
value = jtsGeometryFactory.createMultiPoint(coordinates);
} else {
final com.vividsolutions.jts.geom.Point point = jtsGeometryFactory
.createPoint(new Coordinate(-125, 53));
value = point;
}
} else {
value = "Unknown";
}
}
return value;
}
private final Object plugin;
private final BusinessApplication application;
private Map responseFields;
private SecurityService securityService;
private Map testParameters = new HashMap<>();
private final List