class WidgetLayoutResourceProvider extends AbstractControllerResourceProvider {

  @Override
  public RequestStatus createResources(final Request request)
      throws SystemException,
      UnsupportedPropertyException,
      ResourceAlreadyExistsException,
      NoSuchParentResourceException {

    Set<Resource> associatedResources = new HashSet<>();

    for (final Map<String, Object> properties : request.getProperties()) {
      WidgetLayoutEntity widgetLayoutEntity = createResources(new Command<WidgetLayoutEntity>() {

        @Override
        public WidgetLayoutEntity invoke() throws AmbariException {
          final String[] requiredProperties = {
              WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID,
              WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID,
              WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID,
              WIDGETLAYOUT_SCOPE_PROPERTY_ID,
              WIDGETLAYOUT_WIDGETS_PROPERTY_ID,
              WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID,
              WIDGETLAYOUT_USERNAME_PROPERTY_ID
          };
          for (String propertyName : requiredProperties) {
            if (properties.get(propertyName) == null) {
              throw new AmbariException("Property " + propertyName + " should be provided");
            }
          }
          final WidgetLayoutEntity entity = new WidgetLayoutEntity();
          String userName = getUserName(properties);

          Set widgetsSet = (LinkedHashSet) properties.get(WIDGETLAYOUT_WIDGETS_PROPERTY_ID);

          String clusterName = properties.get(WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).toString();
          entity.setLayoutName(properties.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString());
          entity.setClusterId(getManagementController().getClusters().getCluster(clusterName).getClusterId());
          entity.setSectionName(properties.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString());
          entity.setScope(properties.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString());
          entity.setUserName(userName);
          entity.setDisplayName(properties.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString());

          List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = new LinkedList<>();
          int order=0;
          for (Object widgetObject : widgetsSet) {
            HashMap<String, Object> widget = (HashMap) widgetObject;
            long id = Integer.parseInt(widget.get("id").toString());
            WidgetEntity widgetEntity = widgetDAO.findById(id);
            if (widgetEntity == null) {
              throw new AmbariException("Widget with id " + widget.get("id") + " does not exists");
            }
            WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity = new WidgetLayoutUserWidgetEntity();

            widgetLayoutUserWidgetEntity.setWidget(widgetEntity);
            widgetLayoutUserWidgetEntity.setWidgetOrder(order++);
            widgetLayoutUserWidgetEntity.setWidgetLayout(entity);
            widgetLayoutUserWidgetEntityList.add(widgetLayoutUserWidgetEntity);
            widgetEntity.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity);
          }

          entity.setListWidgetLayoutUserWidgetEntity(widgetLayoutUserWidgetEntityList);
          widgetLayoutDAO.create(entity);
          notifyCreate(Type.WidgetLayout, request);

          return entity;
        }
      });
      Resource resource = new ResourceImpl(Type.WidgetLayout);
      resource.setProperty(WIDGETLAYOUT_ID_PROPERTY_ID, widgetLayoutEntity.getId());
      associatedResources.add(resource);

    }

    return getRequestStatus(null, associatedResources);
  }

  public  WidgetLayoutResourceProvider(AmbariManagementController managementController);

}