class WidgetLayoutResourceProvider extends AbstractControllerResourceProvider {

  @Override
  public RequestStatus updateResources(Request request, Predicate predicate)
    throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException {

    final Set<Map<String, Object>> propertyMaps = request.getProperties();

    modifyResources(new Command<Void>() {
      @Override
      public Void invoke() throws AmbariException {
        for (Map<String, Object> propertyMap : propertyMaps) {
          final Long layoutId;
          try {
            layoutId = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString());
          } catch (Exception ex) {
            throw new AmbariException("WidgetLayout should have numerical id");
          }
          lock.writeLock().lock();
          try {
            final WidgetLayoutEntity entity = widgetLayoutDAO.findById(layoutId);
            if (entity == null) {
              throw new ObjectNotFoundException("There is no widget layout with id " + layoutId);
            }
            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)))) {
              entity.setLayoutName(propertyMap.get(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID).toString());
            }
            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)))) {
              entity.setSectionName(propertyMap.get(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID).toString());
            }
            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)))) {
              entity.setDisplayName(propertyMap.get(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID).toString());
            }
            if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID)))) {
              entity.setScope(propertyMap.get(WIDGETLAYOUT_SCOPE_PROPERTY_ID).toString());
            }

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

            //Remove old relations from widget entities
            for (WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) {
              widgetLayoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity()
                      .remove(widgetLayoutUserWidgetEntity);
            }
            entity.setListWidgetLayoutUserWidgetEntity(new LinkedList<>());

            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.getListWidgetLayoutUserWidgetEntity().add(widgetLayoutUserWidgetEntity);
            }


            widgetLayoutDAO.mergeWithFlush(entity);
          } finally {
            lock.writeLock().unlock();
          }
        }
        return null;
      }
    });

    return getRequestStatus(null);
  }

  public  WidgetLayoutResourceProvider(AmbariManagementController managementController);

}