public int compareTo(LayoutBranch layoutBranch) {
    long primaryKey = layoutBranch.getPrimaryKey();

    if (getPrimaryKey() < primaryKey) {
      return -1;
    } else if (getPrimaryKey() > primaryKey) {
      return 1;
    } else {
      return 0;
    }
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    LayoutBranch layoutBranch = null;

    try {
      layoutBranch = (LayoutBranch) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    long primaryKey = layoutBranch.getPrimaryKey();

    if (getPrimaryKey() == primaryKey) {
      return true;
    } else {
      return false;
    }
  }
  protected LayoutRevision getLayoutRevision() throws Exception {
    Layout layout = LayoutTestUtil.addLayout(_group, false);

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext();

    LayoutSetBranch layoutSetBranch =
        LayoutSetBranchLocalServiceUtil.addLayoutSetBranch(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            false,
            RandomTestUtil.randomString(),
            RandomTestUtil.randomString(),
            true,
            0,
            serviceContext);

    LayoutBranch layoutBranch =
        LayoutBranchLocalServiceUtil.getMasterLayoutBranch(
            layoutSetBranch.getLayoutSetBranchId(), layout.getPlid());

    return LayoutRevisionLocalServiceUtil.getLayoutRevision(
        layoutSetBranch.getLayoutSetBranchId(), layoutBranch.getLayoutBranchId(), layout.getPlid());
  }
  protected void populateElementLayoutMetadata(Element layoutElement, Layout layout)
      throws Exception {

    LayoutStagingHandler layoutStagingHandler = LayoutStagingUtil.getLayoutStagingHandler(layout);

    if (layoutStagingHandler != null) {
      LayoutRevision layoutRevision = layoutStagingHandler.getLayoutRevision();

      if (layoutRevision != null) {
        layoutElement.addAttribute(
            "layout-revision-id", String.valueOf(layoutRevision.getLayoutRevisionId()));
        layoutElement.addAttribute(
            "layout-branch-id", String.valueOf(layoutRevision.getLayoutBranchId()));

        LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();

        layoutElement.addAttribute("layout-branch-name", String.valueOf(layoutBranch.getName()));
      }
    }

    layoutElement.addAttribute("layout-uuid", layout.getUuid());
    layoutElement.addAttribute("layout-id", String.valueOf(layout.getLayoutId()));
    layoutElement.addAttribute("layout-priority", String.valueOf(layout.getPriority()));

    String layoutPrototypeUuid = layout.getLayoutPrototypeUuid();

    if (Validator.isNotNull(layoutPrototypeUuid)) {
      LayoutPrototype layoutPrototype =
          _layoutPrototypeLocalService.getLayoutPrototypeByUuidAndCompanyId(
              layoutPrototypeUuid, layout.getCompanyId());

      layoutElement.addAttribute("layout-prototype-uuid", layoutPrototypeUuid);
      layoutElement.addAttribute(
          "layout-prototype-name", layoutPrototype.getName(LocaleUtil.getDefault()));
    }
  }
  /**
   * Converts the soap model instance into a normal model instance.
   *
   * @param soapModel the soap model instance to convert
   * @return the normal model instance
   */
  public static LayoutBranch toModel(LayoutBranchSoap soapModel) {
    LayoutBranch model = new LayoutBranchImpl();

    model.setLayoutBranchId(soapModel.getLayoutBranchId());
    model.setGroupId(soapModel.getGroupId());
    model.setCompanyId(soapModel.getCompanyId());
    model.setUserId(soapModel.getUserId());
    model.setUserName(soapModel.getUserName());
    model.setLayoutSetBranchId(soapModel.getLayoutSetBranchId());
    model.setPlid(soapModel.getPlid());
    model.setName(soapModel.getName());
    model.setDescription(soapModel.getDescription());
    model.setMaster(soapModel.getMaster());

    return model;
  }
  public static String getLayoutsJSON(
      HttpServletRequest request,
      long groupId,
      boolean privateLayout,
      long parentLayoutId,
      long[] expandedLayoutIds)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    List<Layout> layoutAncestors = null;

    long selPlid = ParamUtil.getLong(request, "selPlid");

    if (selPlid != 0) {
      Layout selLayout = LayoutLocalServiceUtil.getLayout(selPlid);

      layoutAncestors = selLayout.getAncestors();
    }

    List<Layout> layouts = getLayouts(request, groupId, privateLayout, parentLayoutId);

    for (Layout layout : layouts) {
      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      if ((layoutAncestors != null) && layoutAncestors.contains(layout)
          || ArrayUtil.contains(expandedLayoutIds, layout.getLayoutId())) {

        String childrenJSON = StringPool.BLANK;

        if (layout instanceof VirtualLayout) {
          VirtualLayout virtualLayout = (VirtualLayout) layout;

          childrenJSON =
              getLayoutsJSON(
                  request,
                  virtualLayout.getSourceGroupId(),
                  virtualLayout.getPrivateLayout(),
                  virtualLayout.getLayoutId(),
                  expandedLayoutIds);

        } else {
          childrenJSON =
              getLayoutsJSON(
                  request,
                  groupId,
                  layout.getPrivateLayout(),
                  layout.getLayoutId(),
                  expandedLayoutIds);
        }

        jsonObject.put("children", JSONFactoryUtil.createJSONArray(childrenJSON));
      }

      jsonObject.put("contentDisplayPage", layout.isContentDisplayPage());
      jsonObject.put("friendlyURL", layout.getFriendlyURL());

      if (layout instanceof VirtualLayout) {
        VirtualLayout virtualLayout = (VirtualLayout) layout;

        jsonObject.put("groupId", virtualLayout.getSourceGroupId());
      } else {
        jsonObject.put("groupId", layout.getGroupId());
      }

      jsonObject.put("hasChildren", layout.hasChildren());
      jsonObject.put("layoutId", layout.getLayoutId());
      jsonObject.put("name", layout.getName(themeDisplay.getLocale()));
      jsonObject.put("parentLayoutId", layout.getParentLayoutId());
      jsonObject.put("plid", layout.getPlid());
      jsonObject.put("priority", layout.getPriority());
      jsonObject.put("privateLayout", layout.isPrivateLayout());
      jsonObject.put("type", layout.getType());
      jsonObject.put("updateable", SitesUtil.isLayoutUpdateable(layout));
      jsonObject.put("uuid", layout.getUuid());

      LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(layout);

      if (layoutRevision != null) {
        User user = themeDisplay.getUser();

        long recentLayoutSetBranchId =
            StagingUtil.getRecentLayoutSetBranchId(user, layout.getLayoutSet().getLayoutSetId());

        if (StagingUtil.isIncomplete(layout, recentLayoutSetBranchId)) {
          jsonObject.put("incomplete", true);
        }

        long layoutSetBranchId = layoutRevision.getLayoutSetBranchId();

        LayoutSetBranch layoutSetBranch =
            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(layoutSetBranchId);

        LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();

        if (!layoutBranch.isMaster()) {
          jsonObject.put("layoutBranchId", layoutBranch.getLayoutBranchId());
          jsonObject.put("layoutBranchName", layoutBranch.getName());
        }

        jsonObject.put("layoutRevisionId", layoutRevision.getLayoutRevisionId());
        jsonObject.put("layoutSetBranchId", layoutSetBranchId);
        jsonObject.put("layoutSetBranchName", layoutSetBranch.getName());
      }

      jsonArray.put(jsonObject);
    }

    return jsonArray.toString();
  }