Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | package org.onosproject.cord.gui.model; |
| 2 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 3 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 4 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | |
| 6 | import java.util.HashMap; |
| 7 | import java.util.Map; |
| 8 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 9 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
| 10 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Utility factory for operating on XOS functions. |
| 13 | */ |
| 14 | public class XosFunctionFactory extends JsonFactory { |
| 15 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 16 | private static final String PARAMS = "params"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 17 | private static final String LEVEL = "level"; |
| 18 | private static final String LEVELS = "levels"; |
| 19 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 20 | private static final UrlFilterFunction.Level DEFAULT_FILTER_LEVEL = |
| 21 | UrlFilterFunction.Level.PG; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | // no instantiation |
| 25 | private XosFunctionFactory() {} |
| 26 | |
| 27 | /** |
| 28 | * Produces the JSON representation of the given XOS function descriptor. |
| 29 | * |
| 30 | * @param xfd function descriptor |
| 31 | * @return JSON encoding |
| 32 | */ |
| 33 | public static ObjectNode toObjectNode(XosFunctionDescriptor xfd) { |
| 34 | ObjectNode root = objectNode() |
| 35 | .put(ID, xfd.id()) |
| 36 | .put(NAME, xfd.displayName()) |
| 37 | .put(DESC, xfd.description()); |
| 38 | root.set(PARAMS, paramsForXfd(xfd)); |
| 39 | return root; |
| 40 | } |
| 41 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 42 | private static ObjectNode paramsForXfd(XosFunctionDescriptor xfd) { |
| 43 | ParamsFactory psf = PARAM_MAP.get(xfd); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 44 | if (psf == null) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 45 | psf = DEF_PARAMS_FACTORY; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 46 | } |
| 47 | return psf.params(); |
| 48 | } |
| 49 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 50 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 51 | // ==== handling different parameter structures... |
| 52 | private static final Map<XosFunctionDescriptor, ParamsFactory> |
| 53 | PARAM_MAP = new HashMap<XosFunctionDescriptor, ParamsFactory>(); |
| 54 | |
| 55 | private static final ParamsFactory DEF_PARAMS_FACTORY = new ParamsFactory(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 56 | static { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 57 | PARAM_MAP.put(URL_FILTER, new UrlFilterParamsFactory()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Applies a parameter change for the given function, in the context of |
| 62 | * the specified user. |
| 63 | * |
| 64 | * @param xfd function context |
| 65 | * @param userId user identifier |
| 66 | * @param param parameter name |
| 67 | * @param value value to apply |
| 68 | */ |
| 69 | public static void apply(XosFunctionDescriptor xfd, int userId, |
| 70 | String param, String value) { |
| 71 | // TODO: |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates an object node representation of the profile for the |
| 76 | * specified user. |
| 77 | * |
| 78 | * @param user the user |
| 79 | * @return object node profile |
| 80 | */ |
| 81 | public static ObjectNode profileForUser(SubscriberUser user) { |
| 82 | ObjectNode root = objectNode(); |
| 83 | for (XosFunctionDescriptor xfd: XosFunctionDescriptor.values()) { |
| 84 | XosFunction.Memento mem = user.getMemento(xfd); |
| 85 | if (mem != null) { |
| 86 | root.set(xfd.id(), mem.toObjectNode()); |
| 87 | } |
| 88 | } |
| 89 | return root; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // =================================================================== |
| 94 | // === factories for creating parameter structures, both default |
| 95 | // and from a memento... |
| 96 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 97 | // private parameter structure creator |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 98 | static class ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 99 | ObjectNode params() { |
| 100 | return objectNode(); |
| 101 | } |
| 102 | } |
| 103 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 104 | static class UrlFilterParamsFactory extends ParamsFactory { |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 105 | @Override |
| 106 | ObjectNode params() { |
| 107 | ObjectNode result = objectNode(); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 108 | result.put(LEVEL, DEFAULT_FILTER_LEVEL.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 109 | ArrayNode levels = arrayNode(); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 110 | for (UrlFilterFunction.Level lvl: UrlFilterFunction.Level.values()) { |
| 111 | levels.add(lvl.name()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 112 | } |
| 113 | result.set(LEVELS, levels); |
| 114 | return result; |
| 115 | } |
| 116 | } |
| 117 | } |