/** * Sets the {@link PrimaryKey} field of the specified {@link Resource} instance. * * @param resource to set primary key of * @param value to set primary key to * @throws ResourceException */ public static void setPrimaryKey(Resource resource, Object value) throws ResourceException { try { getPrimaryKeyField(resource.getClass()).set(resource, value); } catch (IllegalAccessException e) { throw new ResourceException(e); } }
/** * Returns the {@link PrimaryKey} of the specified {@link Resource} instance. * * @param resource to get pk of * @return primary key * @throws ResourceException */ public static Object getPrimaryKey(Resource resource) throws ResourceException { try { return getPrimaryKeyField(resource.getClass()).get(resource); } catch (IllegalAccessException e) { throw new ResourceException(e); } }
/** * Put's the specified {@link Resource}'s non-null fields into a map and returns the result. * * @param resource to build parameters map for * @return parameter map * @throws IllegalAccessException */ public static Map<String, Object> getParameters(@NotNull Resource resource) throws IllegalAccessException { Class<?> clazz = resource.getClass(); Field[] fields = clazz.getFields(); Map<String, Object> params = new HashMap<>(); for (Field field : fields) { Object value = field.get(resource); if (isResourceField(field) && value != null) params.put(field.getName(), value); } return params; }