修改映射到Java Object的Json结构

时间:2016-07-15 22:07:37

标签: java json spring dictionary filtering

我将这个Json Strucute映射并保存为Java对象:

[
  {
    "name": "users",
    "activationUrl": "users/activation",
    "initialPage": "users/manageUsers",
    "newUntilDate": "2017-01-01",
    "requireActivation": true,
    "menu": {
      "mainMenu": {
        "items": {
          "users": {
            "state": "users.dashboard",
            "i18n": "DASHBOARD",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "dashboard": {
                "state": "invoices.dashboard",
                "i18n": "DASHBOARD",
                "index": 1,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "editUsers",
                  "createUser"
                ]
              }
            }
          }
        }
      },
      "secondaryMenu": {
        "items": {
          "configuration": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "manageUsers": {
                "state": "users.manageUsers",
                "i18n": "USER_MANAGER",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "listUsers",
                  "editUsers",
                  "createUser"
                ]
              },
              "anotherManageUsers": {
                "state": "users.manageUsers",
                "i18n": "USER_MANAGER",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "editUsers",
                  "createUser"
                ]
              }
            }
          },
          "anotherConfiguration": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "manageUsers": {
                "state": "users.manageUsers",
                "i18n": "USER_MANAGER",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "editUsers",
                  "createUser"
                ]
              },
              "anotherManageUsers": {
                "state": "users.manageUsers",
                "i18n": "USER_MANAGER",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "editUsers",
                  "createUser"
                ]
              }
            }
          }
        }
      },
      "usersMenu": {
        "items": {
          "default": {
            "state": null,
            "i18n": null,
            "index": null,
            "iconClass": null,
            "subItems": {
              "signOut": {
                "state": "sign-out",
                "i18n": "Cerrar Sesión",
                "index": 1,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          }
        }
      }
    },
    "defaultActivation": {
      "accounts": [
        "500026",
        "500027"
      ],
      "paymentsModel": {
        "TSP": [
          "CO",
          "AR"
        ],
        "PSP": [
          "CO"
        ]
      }
    }
  },
  {
    "name": "invoices",
    "activationUrl": "invoices/activation",
    "initialPage": "invoices/dashboard",
    "newUntilDate": "2017-01-01",
    "requireActivation": true,
    "menu": {
      "mainMenu": {
        "items": {
          "invoices": {
            "state": null,
            "i18n": "INVOICES",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "dashboard": {
                "state": "invoices.dashboard",
                "i18n": "DASHBOARD",
                "index": 1,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload"
                ]
              },
              "configuration": {
                "state": "invoices.configuration",
                "i18n": "CONFIGURATION",
                "index": 2,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.merchants"
                ]
              },
              "upload": {
                "state": "invoices.upload",
                "i18n": "UPLOAD_INVOICES",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.accounts",
                  "consult",
                  "configure.merchants"
                ]
              },
              "queries": {
                "state": "invoices.queries",
                "i18n": "QUERIES",
                "index": 4,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              },
              "formats": {
                "state": "invoices.formats",
                "i18n": "FORMAT_MANAGEMENT",
                "index": 5,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.invoices",
                  "configure.accounts",
                  "consult"
                ]
              },
              "reports": {
                "state": "invoices.reports",
                "i18n": "SCHEDULE_REPORTS",
                "index": 6,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.invoices",
                  "configure.accounts",
                  "graphics"
                ]
              }
            }
          }
        }
      },
      "secondaryMenu": {
        "items": {
          "help": {
            "state": null,
            "i18n": "HELP",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "reports": {
                "state": "invoices.help",
                "i18n": "INVOICES_REPORTS_HELP",
                "index": 5,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          },
          "configuration": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 2,
            "iconClass": "fa-question-circle",
            "subItems": {}
          },
          "developers": {
            "state": null,
            "i18n": "DEVELOPERS",
            "index": 2,
            "iconClass": "fa-question-circle",
            "subItems": {}
          },
          "notification": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 2,
            "iconClass": "fa-question-circle",
            "subItems": {
              "reports": {
                "state": "invoices.notification",
                "i18n": "INVOICES_NOTIFICATIONS",
                "index": 1,
                "iconClass": null,
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          }
        }
      },
      "usersMenu": {
        "items": {
          "default": {
            "state": null,
            "i18n": null,
            "index": null,
            "iconClass": null,
            "subItems": {
              "signOut": {
                "state": "sign-out",
                "i18n": "Cerrar Sesión",
                "index": 1,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          }
        }
      }
    },
    "defaultActivation": {
      "accounts": [
        "500026",
        "500027"
      ],
      "paymentsModel": {
        "TSP": [
          "CO",
          "AR"
        ],
        "PSP": [
          "CO"
        ]
      }
    }
  }
]

我用帖子坚持下去的方式:

public Module create(@Valid Module module, String moduleUrl)
        throws RestAuthenticationException, JSONException, IOException, BusinessException {

    ...

    dao.save(module);
    return module;
}

模块结构:

@Document(collection = "menus")
public class Module {

    @Indexed(unique = true)
    private String name;

    private String activationUrl;

    private String initialPage;

    private String newUntilDate;

    private Boolean requireActivation;

    private Map<String, Menu> menu;

    private DefaultActivation defaultActivation;

  ...
}

等等。

我需要返回相同的结构,但是从另一个服务获得的配置文件权限过滤只是一个Map,它在json中的表示是这样的:

API /权限?轮廓= “ADMIN”

{
  "invoices.configure.merchants": true,
  "invoices.configure.invoices": true,
  "invoices.configure.accounts": true,
  "invoices.consult": true,
  "invoices.upload": true,
  "invoices.graphics": true,
  "users.listUsers": true,
  "users.editUsers": true,
  "users.createUser": true
}

API /权限?轮廓= “请教”

{
  "invoices.configure.merchants": false,
  "invoices.configure.invoices": false,
  "invoices.configure.accounts": true,
  "invoices.consult": true,
  "invoices.upload": false,
  "invoices.graphics": false,
  "users.listUsers": true,
  "users.editUsers": false,
  "users.createUser": false
}

如果在“功能”中定义了至少一个功能,则从原始结构中删除那些基于字段“featuresToShown”不匹配的元素,对于配置文件“ADMIN”,例如结构保持不变因为所有权限都设置为“true”,但对于“CONSULT”,它必须像:

一样进行过滤
[
  {
    "name": "users",
    "activationUrl": "users/activation",
    "initialPage": "users/manageUsers",
    "newUntilDate": "2017-01-01",
    "requireActivation": true,
    "menu": {
      "secondaryMenu": {
        "items": {
          "configuration": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "manageUsers": {
                "state": "users.manageUsers",
                "i18n": "USER_MANAGER",
                "index": 3,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "listUsers",
                  "editUsers",
                  "createUser"
                ]
              }
            }
          },
        }
      }
    },
    "defaultActivation": {
      "accounts": [
        "500026",
        "500027"
      ],
      "paymentsModel": {
        "TSP": [
          "CO",
          "AR"
        ],
        "PSP": [
          "CO"
        ]
      }
    }
  },
  {
    "name": "invoices",
    "activationUrl": "invoices/activation",
    "initialPage": "invoices/dashboard",
    "newUntilDate": "2017-01-01",
    "requireActivation": true,
    "menu": {
      "mainMenu": {
        "items": {
          "invoices": {
            "state": null,
            "i18n": "INVOICES",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "queries": {
                "state": "invoices.queries",
                "i18n": "QUERIES",
                "index": 4,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              },
              "formats": {
                "state": "invoices.formats",
                "i18n": "FORMAT_MANAGEMENT",
                "index": 5,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.invoices",
                  "configure.accounts",
                  "consult"
                ]
              },
              "reports": {
                "state": "invoices.reports",
                "i18n": "SCHEDULE_REPORTS",
                "index": 6,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "configure.invoices",
                  "configure.accounts",
                  "graphics"
                ]
              }
            }
          }
        }
      },
      "secondaryMenu": {
        "items": {
          "help": {
            "state": null,
            "i18n": "HELP",
            "index": 1,
            "iconClass": "fa-question-circle",
            "subItems": {
              "reports": {
                "state": "invoices.help",
                "i18n": "INVOICES_REPORTS_HELP",
                "index": 5,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          },
          "notification": {
            "state": null,
            "i18n": "CONFIGURATION",
            "index": 2,
            "iconClass": "fa-question-circle",
            "subItems": {
              "reports": {
                "state": "invoices.notification",
                "i18n": "INVOICES_NOTIFICATIONS",
                "index": 1,
                "iconClass": null,
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          }
        }
      },
      "usersMenu": {
        "items": {
          "default": {
            "state": null,
            "i18n": null,
            "index": null,
            "iconClass": null,
            "subItems": {
              "signOut": {
                "state": "sign-out",
                "i18n": "Cerrar Sesión",
                "index": 1,
                "iconClass": "fa-question-circle",
                "featuresToShown": [
                  "upload",
                  "configure.accounts",
                  "consult"
                ]
              }
            }
          }
        }
      }
    },
    "defaultActivation": {
      "accounts": [
        "500026",
        "500027"
      ],
      "paymentsModel": {
        "TSP": [
          "CO",
          "AR"
        ],
        "PSP": [
          "CO"
        ]
      }
    }
  }
]

我这样做的方法是遍历所有结构并创建辅助结构以组装最终的模块并进行验证,但这对于文档中的级别以及许多语句来说都是很多工作。

这段讨厌的代码:

// filters the all the menus by the permissions incoming
for (Map.Entry<String, Boolean> permission : permissions.entrySet()) {
  // returns features with permission for the profile and merchant
  if (permissions.get(permission.getKey())) {
    profilePermissions.add(permission.getKey());
  }
}

// keeps the menu for a merchant and profile
Map<String, Map<String, Menu>> menusProfileMerchant = new HashMap<String, Map<String, Menu>>();

// available modules  
List<Module> modules = getAllModules();
// defines the list of modules with the menus for the profile
List<Module> modulesByProfile = new ArrayList<Module>();

// iterate the module definition
for (Module module : modules) {

  Map<String, Menu> menusProfile = new HashMap<String, Menu>();

  for (Map.Entry<String, Menu> menu : module.getMenu().entrySet()) {
    Map<String, ModuleItem> items = menu.getValue().getItems();

    // keeps the item with features for the profile
    Map<String, ModuleItem> itemsProfile = new HashMap<String, ModuleItem>();

    // iterates over the items and features for a profile
    for (Map.Entry<String, ModuleItem> item : items.entrySet()) {

      /** the variable to validate if the features is contained for the profile */
      boolean contains = false;

      for (String permission : profilePermissions) {

        // keeps the module name from profile permissions
        String permissionModule = permission.substring(0,
            permission.indexOf(dotSeparator));

        // keeps the permissions from profile permissions
        String permissionFeature = permission
            .substring(permission.indexOf(dotSeparator) + initialIndex);

        // defines the containing feature for the module and profile
        Map<String, ModuleSubItem> subItems = item.getValue().getSubItems();

        Map<String, ModuleSubItem> subItemsProfile = new HashMap<String, ModuleSubItem>();

        if (subItems != null) {

          for (Map.Entry<String, ModuleSubItem> subItem : subItems.entrySet()) {

            if (permissionModule.equals(module.getName()) && subItem.getValue()
                .getFeaturesToShown().contains(permissionFeature)) {
              contains = true;
            }
          }

          // adds the features for the menus by profile
          if (contains) {
            itemsProfile.put(item.getKey(), item.getValue());
          }

        }

      }



    }

    // sets the items defined just for a profile with permissions over them
    menu.getValue().setItems(itemsProfile);
    menusProfile.put(menu.getKey(), menu.getValue());

  }

  // add the module menu just for a profile with permissions over it
  menusProfileMerchant.put(module.getName(), menusProfile);
  modulesByProfile.add(module);
}

// returns the list of modules with the menus for the profile
return modulesByProfile;
}

我找不到更高效的东西。它可以用更干净的方式完成吗?我正在使用Java 8。

0 个答案:

没有答案