protected void executeLogic(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws Exception {
   AccountFB fb = (AccountFB) form;
   UserAccount account =
       (UserAccount) getEnvironment().getEntityFactory().create("gallery-useraccount");
   PropertyUtils.copyProperties(account, fb);
   getEnvironment().getEntityStorageFactory().getStorage("gallery-useraccount").store(account);
   if (account.getStylesheet() != null && account.getStylesheet().length() > 0) {
     request.getSession().setAttribute("stylesheetPB", account.getStylesheet());
   } else {
     request.getSession().removeAttribute("stylesheetPB");
   }
   if (account.getUsername().equals(request.getRemoteUser())) {
     boolean useEnglish =
         !request
             .getLocale()
             .getLanguage()
             .equals(getEnvironment().getConfigurableResources().getParameter("nativelanguage"));
     String title = account.getTitle();
     if (useEnglish && StringUtil.asNull(account.getTitleEnglish()) != null) {
       title = account.getTitleEnglish();
     }
     request.getSession().setAttribute("titlePB", title);
   }
 }
  protected void executeLogic(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {
    SelectPictureFB fb = (SelectPictureFB) form;
    Integer virtualGalleryId = fb.getGallery();
    setVirtualGalleryId(request, virtualGalleryId);
    Gallery gallery = GalleryHelper.getGallery(getEnvironment(), virtualGalleryId);
    if (gallery == null) {
      saveErrors(request, Arrays.asList(new String[] {"gallery.gallery.gallery-not-found"}));
      return;
    }
    setGallery(request, gallery);
    String language = fb.getLanguage();
    if (StringUtil.asNull(language) == null) {
      language = request.getLocale().getLanguage();
    }
    boolean useEnglish =
        !language.equals(
            getEnvironment().getConfigurableResources().getParameter("nativelanguage"));
    Integer galleryId = GalleryHelper.getGalleryId(gallery);
    setGalleryId(request, galleryId);
    Integer categoryId = fb.getCategory();
    setCategoryId(request, categoryId);
    LOG.debug("Search pictures: " + fb.getGallery() + "," + fb.getCategory());
    initRequestParameters(request);
    Collection categories = getCategories(request, form);
    Collection picturesAllowed = getPictures(request, form);
    QueryFilter filter;
    Integer max = fb.getMax();
    if (max == null) {
      if (gallery.getNoOfCols() != null
          && gallery.getNoOfCols().intValue() > 0
          && gallery.getNoOfRows() != null
          && gallery.getNoOfRows().intValue() > 0) {
        max = new Integer(gallery.getNoOfCols().intValue() * gallery.getNoOfRows().intValue());
      }
    }
    if (fb.getStart() != null && max != null && max.intValue() != 0) {
      if (picturesAllowed != null) {
        if (categories != null) {
          filter = new QueryFilter(getCategoryTreeFilter(request) + "andpicturelist" + "withlimit");
          filter.setAttribute("categories", categories);
        } else {
          filter = new QueryFilter(getAllFilter(request) + "andpicturelist" + "withlimit");
        }
        filter.setAttribute("pictures", picturesAllowed);
      } else {
        if (categories != null) {
          filter = new QueryFilter(getCategoryTreeFilter(request) + "withlimit");
          filter.setAttribute("categories", categories);
        } else {
          filter = new QueryFilter(getAllFilter(request) + "withlimit");
        }
      }
      filter.setAttribute("start", fb.getStart());
      if (max != null) {
        filter.setAttribute("max", max);
      }
    } else {
      if (picturesAllowed != null) {
        if (categories != null) {
          filter = new QueryFilter(getCategoryTreeFilter(request) + "andpicturelist");
          filter.setAttribute("categories", categories);
        } else {
          filter = new QueryFilter(getAllFilter(request) + "andpicturelist");
        }
        filter.setAttribute("pictures", picturesAllowed);
      } else {
        if (categories != null) {
          filter = new QueryFilter(getCategoryTreeFilter(request));
          filter.setAttribute("categories", categories);
        } else {
          filter = new QueryFilter(getAllFilter(request));
        }
      }
    }
    filter.setAttribute("gallery", galleryId);
    filter.setOrderName(gallery.getSortOrder());
    setFilterAttributes(request, form, filter);
    EntityInterface[] entities = new EntityInterface[0];
    if (picturesAllowed == null || (picturesAllowed != null && picturesAllowed.size() > 0)) {
      entities =
          getEnvironment().getEntityStorageFactory().getStorage("gallery-picture").search(filter);
    }

    String username = gallery.getUsername();
    // String username = fb.getUser();
    // if (username == null || username.length() == 0) {
    //  username = request.getRemoteUser();
    // }
    filter = new QueryFilter("allforuser");
    filter.setAttribute("username", username);
    EntityInterface[] storageEntities =
        getEnvironment()
            .getEntityStorageFactory()
            .getStorage("gallery-picturestorage")
            .search(filter);

    if (gallery.getMaxWidth() != null && gallery.getMaxWidth().intValue() > 0) {
      filter = new QueryFilter("all-smaller-than");
      filter.setAttribute("width", gallery.getMaxWidth());
    } else {
      filter = new QueryFilter("all");
    }
    EntityInterface[] resolutionEntities =
        getEnvironment().getEntityStorageFactory().getStorage("gallery-resolution").search(filter);
    ResolutionPB[] resolutionsPB = null;
    Integer defaultResolution = null;
    if (resolutionEntities.length > 0) {
      for (int i = 0; i < resolutionEntities.length; i++) {
        if (((Resolution) resolutionEntities[i])
            .getId()
            .equalsIgnoreCase(gallery.getDefaultResolution())) {
          defaultResolution = ((Resolution) resolutionEntities[i]).getWidth();
          break;
        }
      }
      resolutionsPB = new ResolutionPB[resolutionEntities.length];
      for (int i = 0; i < resolutionsPB.length; i++) {
        resolutionsPB[i] = new ResolutionPB();
        try {
          PropertyUtils.copyProperties(resolutionsPB[i], resolutionEntities[i]);
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        } catch (NoSuchMethodException e) {
        }
      }
    }

    Map pictureParameters = new HashMap();
    if (StringUtil.asNull(request.getServerName()) != null) {
      pictureParameters.put("hostname", request.getServerName());
      if (request.getServerPort() != 80) {
        pictureParameters.put("port", new Integer(request.getServerPort()));
      }
    }
    pictureParameters.put("contextpath", request.getContextPath());
    pictureParameters.put("user", username);
    PicturePB[] picturesPB = new PicturePB[entities.length];
    ActionForward updateForward = mapping.findForward("picture-update-link");
    ActionForward removeForward = mapping.findForward("picture-remove-link");
    ActionForward resolutionForward = mapping.findForward("picture-resolution-link");
    ActionForward pictureLinkForward = mapping.findForward("picture-link");
    ActionForward pictureImageForward = mapping.findForward("picture-image");
    if (defaultResolution != null) {
      pictureParameters.put("width", defaultResolution.toString());
    }
    if (StringUtil.asNull(request.getParameter("thumbnailwidth")) != null) {
      pictureParameters.put("thumbnailwidth", request.getParameter("thumbnailwidth"));
    } else if (gallery.getThumbnailWidth() != null && gallery.getThumbnailWidth().intValue() > 0) {
      pictureParameters.put("thumbnailwidth", gallery.getThumbnailWidth());
    }
    if (StringUtil.asNull(request.getParameter("thumbnailheight")) != null) {
      pictureParameters.put("thumbnailheight", request.getParameter("thumbnailheight"));
    } else if (gallery.getThumbnailHeight() != null
        && gallery.getThumbnailHeight().intValue() > 0) {
      pictureParameters.put("thumbnailheight", gallery.getThumbnailHeight());
    }
    List thumbnailInfoList = new ArrayList();
    if (StringUtil.asNull(gallery.getThumbnailRow1()) != null) {
      thumbnailInfoList.add(gallery.getThumbnailRow1());
    }
    if (StringUtil.asNull(gallery.getThumbnailRow2()) != null) {
      thumbnailInfoList.add(gallery.getThumbnailRow2());
    }
    if (StringUtil.asNull(gallery.getThumbnailRow3()) != null) {
      thumbnailInfoList.add(gallery.getThumbnailRow3());
    }
    String[] thumbnailInfo = (String[]) thumbnailInfoList.toArray(new String[0]);
    JPEGMetadataHandler jpegHandler = new JPEGMetadataHandler(false);
    FileMetadataHandler fileHandler = new FileMetadataHandler(false);

    Integer currentStart = fb.getStart();
    if (currentStart == null) {
      currentStart = new Integer(0);
    }
    Map currentLinkPictureParameters = new HashMap();
    Map currentParameters = new HashMap();
    currentParameters.put("user", username);
    if (StringUtil.asNull(request.getServerName()) != null) {
      currentParameters.put("hostname", request.getServerName());
      if (request.getServerPort() != 80) {
        currentParameters.put("port", new Integer(request.getServerPort()));
      }
    }
    currentParameters.put("contextpath", request.getContextPath());
    Enumeration paramEnum = request.getParameterNames();
    while (paramEnum.hasMoreElements()) {
      String parameter = (String) paramEnum.nextElement();
      currentParameters.put(parameter, request.getParameter(parameter));
    }
    ActionForward currentForward = mapping.findForward("current-link");
    for (int i = 0; i < entities.length; i++) {
      picturesPB[i] = new PicturePB();
      Picture picture = (Picture) entities[i];
      if (StringUtil.asNull(picture.getFile()) != null) {
        picture.setFile(getImagePath(storageEntities, picture.getFile()));
      }
      PropertyUtils.copyProperties(picturesPB[i], entities[i]);
      if (useEnglish && StringUtil.asNull(picture.getTitleEnglish()) != null) {
        picturesPB[i].setTitle(picture.getTitleEnglish());
      }
      if (useEnglish && StringUtil.asNull(picture.getDescriptionEnglish()) != null) {
        picturesPB[i].setDescription(picture.getDescriptionEnglish());
      }
      pictureParameters.put("gallery", virtualGalleryId);
      pictureParameters.put("picture", picturesPB[i].getId());
      if (picturesPB[i].getImage().startsWith("{")) {
        String path = pictureImageForward.getPath();
        picturesPB[i].setImage(
            ServletParameterHelper.replaceDynamicParameters(path, pictureParameters));
      } else {
        picturesPB[i].setImage(getImagePath(storageEntities, picturesPB[i].getImage()));
      }
      if (gallery.getUsername().equals(request.getRemoteUser())) {
        if (updateForward != null) {
          String path = updateForward.getPath();
          picturesPB[i].setUpdateLink(
              ServletParameterHelper.replaceDynamicParameters(path, pictureParameters));
        }
        if (removeForward != null) {
          String path = removeForward.getPath();
          picturesPB[i].setRemoveLink(
              ServletParameterHelper.replaceDynamicParameters(path, pictureParameters));
        }
      } else {
        if (gallery.getShowResolutionLinks() != null
            && gallery.getShowResolutionLinks().booleanValue()
            && resolutionForward != null) {
          ResolutionLinkPB[] resolutionLinksPB = new ResolutionLinkPB[resolutionsPB.length];
          Map resolutionParameters = new HashMap();
          resolutionParameters.put("gallery", virtualGalleryId);
          resolutionParameters.put("picture", picturesPB[i].getId());
          for (int j = 0; j < resolutionLinksPB.length; j++) {
            resolutionParameters.put("width", resolutionsPB[j].getWidth());
            String path =
                ServletParameterHelper.replaceDynamicParameters(
                    resolutionForward.getPath(), resolutionParameters);
            resolutionLinksPB[j] =
                new ResolutionLinkPB(
                    resolutionsPB[j].getId(), resolutionsPB[j].getDescription(), path);
          }
          picturesPB[i].setResolutions(resolutionLinksPB);
        }
      }
      if (picturesPB[i].getLink().startsWith("{")) {
        String path = null;
        if (defaultResolution != null) {
          if (resolutionForward != null) {
            path = resolutionForward.getPath();
          }
        }
        if (path == null) {
          path = pictureLinkForward.getPath();
        }
        picturesPB[i].setLink(
            ServletParameterHelper.replaceDynamicParameters(path, pictureParameters));
      } else {
        picturesPB[i].setLink(getImagePath(storageEntities, picturesPB[i].getLink()));
      }
      if (gallery.getShowPictureTitle() != null && !gallery.getShowPictureTitle().booleanValue()) {
        picturesPB[i].setTitle(null);
      }
      picturesPB[i].setGallery(virtualGalleryId);
      if (picturesPB[i].getTitle() != null
          && gallery.getUseShortPictureNames() != null
          && gallery.getUseShortPictureNames().booleanValue()) {
        picturesPB[i].setTitle(((Picture) entities[i]).getShortTitle());
      }
      if (StringUtil.asNull(gallery.getThumbnailPictureTitle()) != null) {
        picturesPB[i].setTitle(
            getPictureInfo(
                gallery.getThumbnailPictureTitle(),
                picture,
                picturesPB[i],
                jpegHandler,
                fileHandler));
      }
      if (picturesPB[i].getTitle() != null
          && gallery.getCutLongPictureTitles() != null
          && gallery.getCutLongPictureTitles().booleanValue()) {
        if (picturesPB[i].getTitle() != null && picturesPB[i].getTitle().length() > 30) {
          picturesPB[i].setTitle(
              "..." + picturesPB[i].getTitle().substring(picturesPB[i].getTitle().length() - 27));
        }
      }
      if (thumbnailInfo.length > 0) {
        picturesPB[i].setRow1Info(
            getPictureInfo(thumbnailInfo[0], picture, picturesPB[i], jpegHandler, fileHandler));
      }
      if (thumbnailInfo.length > 1) {
        picturesPB[i].setRow2Info(
            getPictureInfo(thumbnailInfo[1], picture, picturesPB[i], jpegHandler, fileHandler));
      }
      if (thumbnailInfo.length > 2) {
        picturesPB[i].setRow3Info(
            getPictureInfo(thumbnailInfo[2], picture, picturesPB[i], jpegHandler, fileHandler));
      }
      if (currentForward != null) {
        currentLinkPictureParameters.clear();
        currentLinkPictureParameters.putAll(pictureParameters);
        currentLinkPictureParameters.putAll(currentParameters);
        currentLinkPictureParameters.put("start", currentStart.toString());
        picturesPB[i].setCurrentLink(
            ServletParameterHelper.replaceDynamicParameters(
                currentForward.getPath(), currentLinkPictureParameters));
        currentStart = new Integer(currentStart.intValue() + 1);
      }
    }
    PictureCollectionPB pb = new PictureCollectionPB();
    pb.setPictures(picturesPB);

    Map parameters = new HashMap();
    parameters.putAll(currentParameters);
    addParameters(parameters, fb);
    Integer prevStart = getPreviousPage(fb.getStart(), max);
    if (prevStart != null) {
      parameters.put("start", prevStart.toString());
      ActionForward forward = mapping.findForward("previous-link");
      if (forward != null) {
        pb.setPrevLink(
            ServletParameterHelper.replaceDynamicParameters(forward.getPath(), parameters));
      }
    } else {
      pb.setPrevLink(null);
    }
    Integer nextStart = getNextPage(fb.getStart(), max, picturesPB.length);
    if (nextStart != null) {
      parameters.put("start", nextStart.toString());
      ActionForward forward = mapping.findForward("next-link");
      if (forward != null) {
        pb.setNextLink(
            ServletParameterHelper.replaceDynamicParameters(forward.getPath(), parameters));
      }
    } else {
      pb.setNextLink(null);
    }

    currentStart = fb.getStart();
    if (currentStart == null) {
      currentStart = new Integer(0);
    }
    parameters.put("start", currentStart.toString());
    if (currentForward != null) {
      pb.setCurrentLink(
          ServletParameterHelper.replaceDynamicParameters(currentForward.getPath(), parameters));
    }

    ActionForward forward = null;
    if (gallery.getAllowSearch() != null && gallery.getAllowSearch().booleanValue()) {
      forward = mapping.findForward("search-link");
    } else {
      forward = mapping.findForward("search-private-link");
    }
    if (forward != null) {
      pb.setSearchLink(
          ServletParameterHelper.replaceDynamicParameters(forward.getPath(), parameters));
    }
    forward = mapping.findForward("page-link");
    if (forward != null) {
      pb.setPageLink(
          ServletParameterHelper.replaceDynamicParameters(forward.getPath(), parameters));
    }
    request.setAttribute("picturesPB", pb);

    GalleryPB galleryPB = new GalleryPB();
    PropertyUtils.copyProperties(galleryPB, gallery);
    if (useEnglish && StringUtil.asNull(gallery.getTitleEnglish()) != null) {
      galleryPB.setTitle(gallery.getTitleEnglish());
    }
    if (useEnglish && StringUtil.asNull(gallery.getDescriptionEnglish()) != null) {
      galleryPB.setDescription(gallery.getDescriptionEnglish());
    }
    galleryPB.setVirtual(Boolean.valueOf(!virtualGalleryId.equals(galleryId)));
    if (galleryPB.getNoOfCols() != null && galleryPB.getNoOfCols().intValue() <= 0) {
      galleryPB.setNoOfCols(null);
    }
    if (galleryPB.getNoOfRows() != null && galleryPB.getNoOfRows().intValue() <= 0) {
      galleryPB.setNoOfRows(null);
    }
    if (max == null
        || max.intValue() == 0
        || (gallery.getNoOfCols() != null
            && gallery.getNoOfRows() != null
            && (gallery.getNoOfRows().intValue() * gallery.getNoOfCols().intValue())
                < max.intValue())) {
      galleryPB.setNoOfRows(null);
    }
    galleryPB.setNoOfThumnailInfoRows(
        thumbnailInfo.length > 0 ? new Integer(thumbnailInfo.length + 1) : new Integer(1));
    request.setAttribute("galleryPB", galleryPB);
    String skin = gallery.getSkin();
    if (StringUtil.asNull(fb.getSkin()) != null) {
      skin = fb.getSkin();
    }
    if (mapping.getParameter() != null && mapping.getParameter().equals("useskin")) {
      SkinFB previous = (SkinFB) request.getSession().getAttribute("skinPB");
      if (StringUtil.asNull(skin) == null) {
        UserAccount template =
            (UserAccount) getEnvironment().getEntityFactory().create("gallery-useraccount");
        template.setUsername(username);
        UserAccount account =
            (UserAccount)
                getEnvironment()
                    .getEntityStorageFactory()
                    .getStorage("gallery-useraccount")
                    .load(template);
        skin = account.getSkin();
      }
      if (previous == null || previous.getId() == null || !previous.getId().equals(skin)) {
        SkinFB pbSkin = SkinHelper.loadSkin(mapping, skin);
        request.getSession().setAttribute("skinPB", pbSkin);
      }
    }
    SkinFB pbSkin = (SkinFB) request.getSession().getAttribute("skinPB");
    if (pbSkin == null || StringUtil.asNull(pbSkin.getStylesheet()) == null) {
      if (gallery != null
          && gallery.getStylesheet() != null
          && gallery.getStylesheet().length() > 0) {
        request.getSession().setAttribute("stylesheetPB", gallery.getStylesheet());
      } else {
        request.getSession().removeAttribute("stylesheetPB");
      }
    } else {
      request.getSession().setAttribute("stylesheetPB", pbSkin.getStylesheet());
    }
    String pbTitle = (String) request.getSession().getAttribute("titlePB");
    if (StringUtil.asNull(pbTitle) == null) {
      UserAccount template =
          (UserAccount) getEnvironment().getEntityFactory().create("gallery-useraccount");
      template.setUsername(username);
      UserAccount account =
          (UserAccount)
              getEnvironment()
                  .getEntityStorageFactory()
                  .getStorage("gallery-useraccount")
                  .load(template);
      String title = account.getTitle();
      if (useEnglish && StringUtil.asNull(account.getTitleEnglish()) != null) {
        title = account.getTitleEnglish();
      }
      request.getSession().setAttribute("titlePB", title);
    }
  }