@Override public Result execute(InteractionContext ctx) throws InteractionException { assert (ctx != null); // retrieve from a database, etc. String id = ctx.getId(); Customer customer = daoHibernate.getCustomer(id); if (customer != null) { // Convert Customer object into Entity object EntityProperties addressFields = new EntityProperties(); addressFields.setProperty( new EntityProperty("postcode", customer.getAddress().getPostcode())); addressFields.setProperty( new EntityProperty("houseNumber", customer.getAddress().getHouseNumber())); EntityProperties props = new EntityProperties(); props.setProperty(new EntityProperty("name", customer.getName())); props.setProperty(new EntityProperty("address", addressFields)); props.setProperty(new EntityProperty("dateOfBirth", customer.getDateOfBirth())); Entity entity = new Entity("Customer", props); ctx.setResource(createEntityResource(entity)); return Result.SUCCESS; } else { return Result.FAILURE; } }
public boolean matches(Object o) { if (o instanceof InteractionContext) { InteractionContext ctx = (InteractionContext) o; if (ctx.getResource() == null) return false; return true; } return false; }
public boolean matches(Object o) { if (o instanceof InteractionContext) { InteractionContext ctx = (InteractionContext) o; MultivaluedMap<String, String> mvmap = ctx.getQueryParameters(); if (!mvmap.getFirst("$filter").equals("this+that")) { return false; } return true; } return false; }
/* * Query method for interaction context parameters returning raw sql data. * * If given a key will return a single row. * * If given a null key will return all rows. */ public SqlRowSet query(String tableName, String key, InteractionContext ctx) throws UnsupportedQueryOperationException, JdbcException, Exception { // Not much point selecting from a null table if (null == tableName) { logger.error("Jdbc producer cannot select from null table."); throw (new JdbcException(Status.INTERNAL_SERVER_ERROR, "Null table name")); } // Get column types from Jdbc. We need these both for constructing the // command and processing it's result set. // We need the primary key for row ordering. // TODO Eventually this should be cached. ColumnTypesMap colTypesMap = new ColumnTypesMap(this, tableName, true); // Unpack the commands $filter and $select terms. AccessProfile accessProfile = getAccessProfile(ctx); // Get top and skip parameters (null if not specified). MultivaluedMap<String, String> queryParams = ctx.getQueryParameters(); String top = queryParams.getFirst(ODataParser.TOP_KEY); String skip = queryParams.getFirst(ODataParser.SKIP_KEY); List<OrderBy> orderBy = ODataParser.parseOrderBy(queryParams.getFirst(ODataParser.ORDERBY_KEY)); // Build an SQL command SqlCommandBuilder sqlBuilder = new SqlCommandBuilder( tableName, key, accessProfile, colTypesMap, top, skip, orderBy, serverMode); String sqlCommand = sqlBuilder.getCommand(); logger.info("Jdbc producer about to execute \"" + sqlCommand + "\""); // Execute the SQL command return query(sqlCommand); }
/** * Returns a view action property This method will try to replace template parameters "{param}" * with query parameters if available * * @param ctx interaction context * @param property action property * @return property value */ public static String getViewActionProperty(InteractionContext ctx, String property) { String prop = null; if (ctx.getCurrentState().getViewAction() != null) { Properties properties = ctx.getCurrentState().getViewAction().getProperties(); if (properties != null && properties.containsKey(property)) { // Get the specified action property prop = getActionProperty(property, properties, ctx.getQueryParameters()); // Fill in template parameters if (prop != null) { Matcher m = parameterPattern.matcher(prop); while (m.find()) { String templateParam = m.group(1); // e.g. code if (ctx.getQueryParameters().containsKey(templateParam)) { prop = prop.replaceAll( "\\{" + templateParam + "\\}", ctx.getQueryParameters().getFirst(templateParam)); } else if (ctx.getPathParameters().containsKey(templateParam)) { prop = prop.replaceAll( "\\{" + templateParam + "\\}", ctx.getPathParameters().getFirst(templateParam)); } } } } } return prop != null && !prop.equals(property) ? prop : null; }