gt:         Map params = new HashMap();
context: /**
   * Copyright (c) 2009--2010 Red Hat, Inc.
   *
   * This software is licensed to you under the GNU General Public License,
   * version 2 (GPLv2). There is NO WARRANTY for this software, express or
   * implied, including the implied warranties of MERCHANTABILITY or FITNESS
   * FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
   * along with this software; if not, see
   * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
   *
   * Red Hat trademarks are not licensed under GPLv2. No permission is
   * granted to use or replicate Red Hat trademarks that are incorporated
   * in this software or its documentation.
   */
  package com.redhat.rhn.manager.errata;
  
  import com.redhat.rhn.common.conf.Config;
  import com.redhat.rhn.common.conf.ConfigDefaults;
  import com.redhat.rhn.common.db.datasource.DataResult;
  import com.redhat.rhn.common.db.datasource.ModeFactory;
  import com.redhat.rhn.common.db.datasource.SelectMode;
  import com.redhat.rhn.common.db.datasource.WriteMode;
  import com.redhat.rhn.common.hibernate.HibernateFactory;
  import com.redhat.rhn.common.hibernate.LookupException;
  import com.redhat.rhn.common.localization.LocalizationService;
  import com.redhat.rhn.common.messaging.MessageQueue;
  import com.redhat.rhn.common.security.PermissionException;
  import com.redhat.rhn.domain.action.Action;
  import com.redhat.rhn.domain.channel.Channel;
  import com.redhat.rhn.domain.channel.ChannelFactory;
  import com.redhat.rhn.domain.errata.Bug;
  import com.redhat.rhn.domain.errata.Errata;
  import com.redhat.rhn.domain.errata.ErrataFactory;
  import com.redhat.rhn.domain.errata.ErrataFile;
  import com.redhat.rhn.domain.errata.impl.PublishedClonedErrata;
  import com.redhat.rhn.domain.org.Org;
  import com.redhat.rhn.domain.rhnset.RhnSet;
  import com.redhat.rhn.domain.role.RoleFactory;
  import com.redhat.rhn.domain.user.User;
  import com.redhat.rhn.frontend.dto.ChannelOverview;
  import com.redhat.rhn.frontend.dto.ErrataOverview;
  import com.redhat.rhn.frontend.dto.OwnedErrata;
  import com.redhat.rhn.frontend.dto.PackageOverview;
  import com.redhat.rhn.frontend.events.CloneErrataAction;
  import com.redhat.rhn.frontend.events.CloneErrataEvent;
  import com.redhat.rhn.frontend.listview.PageControl;
  import com.redhat.rhn.frontend.xmlrpc.InvalidErrataException;
  import com.redhat.rhn.manager.BaseManager;
  import com.redhat.rhn.manager.action.ActionManager;
  import com.redhat.rhn.manager.channel.ChannelManager;
  import com.redhat.rhn.manager.errata.cache.ErrataCacheManager;
  import com.redhat.rhn.manager.rhnset.RhnSetDecl;
  import com.redhat.rhn.manager.rhnset.RhnSetManager;
  import com.redhat.rhn.manager.system.SystemManager;
  
  import org.apache.commons.lang.StringUtils;
  import org.apache.log4j.Logger;
  
  import java.io.File;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Date;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import redstone.xmlrpc.XmlRpcClient;
  import redstone.xmlrpc.XmlRpcFault;
  
  /**
   * ErrataManager is the singleton class used to provide business operations
   * on Errata, where those operations interact with other top tier business
   * objects.  Operations that require changes to the Errata.
   * @version $Rev$
   */
  public class ErrataManager extends BaseManager {
  
      private static Logger log = Logger.getLogger(ErrataManager.class);
      public static final String DATE_FORMAT_PARSE_STRING = "yyyy-MM-dd";
      public static final long MAX_ADVISORY_RELEASE = 9999;
  
      private ErrataManager() {
      }
  
      /**
       * Converts a list of ErrataFile instances into java.io.File instances
       * If a corresponding java.io.File instance is not found for a given
       * ErrataFile instance then it is skipped and not added to the returned list.
       * @param errataFiles list of files to resolve
       * @return list of corresponding java.io.File instances
       */
      public static List resolveOvalFiles(List errataFiles) {
          if (errataFiles == null || errataFiles.size() == 0) {
              return null;
          }
          List retval = new LinkedList();
          for (Iterator iter = errataFiles.iterator(); iter.hasNext();) {
              String directory = Config.get().getString("web.mount_point");
              ErrataFile ef = (ErrataFile) iter.next();
              if (directory == null) {
                  return null;
              }
              if (!directory.endsWith("/")) {
                  directory += "/";
              }
              directory += "rhn/errata/oval/";
              String fileName = ef.getFileName();
              if (!fileName.toLowerCase().startsWith(directory)) {
                  fileName = directory + fileName;
              }
              File f = new File(fileName);
              if (f.exists()) {
                  retval.add(f);
              }
          }
          return retval;
      }
  
      /**
       * Tries to locate errata based on either the errataum's id or the
       * CVE/CAN identifier string.
       * @param identifier erratum id or CVE/CAN id string
       * @return list of erratas found
       */
      public static List lookupErrataByIdentifier(String identifier) {
          return ErrataFactory.lookupByIdentifier(identifier);
      }
  
      /**
       * Takes an unpublished errata and returns a published errata into the
       * channels we pass in. NOTE:  This method does NOT update the errata cache for
       * the channels.  That is done when packages are pushed as part of the errata
       * publication process (which is not done here)
       *
       * @param unpublished The errata to publish
       * @param channelIds The Long channelIds we want to publish this Errata to.
       * @param user who is publishing errata
       * @return Returns a published errata.
       */
      public static Errata publish(Errata unpublished, Collection channelIds, User user) {
          //pass on to the factory
          Errata retval = ErrataFactory.publish(unpublished);
          log.debug("publish - errata published");
  
          retval = addChannelsToErrata(retval, channelIds, user);
          log.debug("publish - updateErrataCacheForChannelsAsync called");
  
          // update the search server
          updateSearchIndex();
          return retval;
      }
  
      /**
       * Takes an unpublished errata and returns a published errata
       *
       * @param unpublished The errata to publish
       * @return Returns a published errata.
       */
      public static Errata publish(Errata unpublished) {
          //pass on to the factory
          Errata retval = ErrataFactory.publish(unpublished);
          log.debug("publish - errata published");
  
          // update the search server
          updateSearchIndex();
          return retval;
      }
  
      /**
       * Add the channels in the channelIds set to the passed in errata.
       *
       * @param errata to add channels to
       * @param channelIds to add
       * @param user who is adding channels to errata
       * @return Errata that is reloaded from the DB.
       */
      public static Errata addChannelsToErrata(Errata errata,
                          Collection<Long> channelIds, User user) {
          log.debug("addChannelsToErrata");
          Iterator itr = channelIds.iterator();
  
          while (itr.hasNext()) {
              Long channelId = (Long) itr.next();
              Channel channel = ChannelManager.lookupByIdAndUser(channelId, user);
              if (channel != null) {
                  errata.addChannel(channel);
              }
          }
  
          //if we're publishing the errata but not pushing packages
          //  We need to add cache entries for ones that are already in the channel
          //  and associated to the errata
          List<Long> list = new ArrayList<Long>();
          list.addAll(channelIds);
          ErrataCacheManager.insertCacheForChannelErrata(list, errata);
  
  
          //Save the errata
          log.debug("addChannelsToErrata - storing errata");
          storeErrata(errata);
  
          errata = (Errata) HibernateFactory.reload(errata);
          log.debug("addChannelsToErrata - errata reloaded from DB");
          return errata;
      }
  
      /**
       * Creates a new (Unpublished) Errata object.
       * @return Returns a fresh errata
       */
      public static Errata createNewErrata() {
          return ErrataFactory.createUnpublishedErrata();
      }
  
      /**
       * Creates a new Unpublished Bug with the id and summary given.
       * @param id The id for the new bug.
       * @param summary The summary for the new bug.
       * @return Returns a Bug object.
       */
      public static Bug createNewUnpublishedBug(Long id, String summary) {
          return ErrataFactory.createUnpublishedBug(id, summary);
      }
  
      /**
       * Creates a new PublishedBug with the id and summary given.
       * @param id The id for the new bug
       * @param summary The summary for the new bug
       * @return Returns a Bug object
       */
      public static Bug createNewPublishedBug(Long id, String summary) {
          return ErrataFactory.createPublishedBug(id, summary);
      }
  
      /**
       * Returns all of the errata.
       * @param user Currently logged in user.
       * @return all of the errata.
       */
      public static DataResult allErrata(User user) {
          SelectMode m = ModeFactory.getMode("Errata_queries", "all_errata");
          Map params = new HashMap();
          params.put("org_id", user.getOrg().getId());
          Map elabParams = new HashMap();
          elabParams.put("user_id", user.getId());
          DataResult result = makeDataResult(params, elabParams, null, m);
          return result;
      }
  
      /**
       * Returns all of the errata of specified advisory type.
       * @param user Currently logged in user.
       * @param type advisory type
       * @return all errata of specified advisory type
       */
      public static DataResult allErrataByType(User user, String type) {
          SelectMode m = ModeFactory.getMode("Errata_queries", "all_errata_by_type");
          Map params = new HashMap();
          params.put("org_id", user.getOrg().getId());
          params.put("type", type);
          Map elabParams = new HashMap();
          elabParams.put("user_id", user.getId());
          DataResult result = makeDataResult(params, elabParams, null, m);
          return result;
      }
  
      /**
       * Returns all of the security errata
       * @param user Currently logged in user.
       * @return all security errata
       */
      public static DataResult allSecurityErrata(User user) {
          SelectMode m = ModeFactory.getMode("Errata_queries",
                          "all_errata_by_type_with_cves");
          Map params = new HashMap();
          params.put("org_id", user.getOrg().getId());
          params.put("type", ErrataFactory.ERRATA_TYPE_SECURITY);
          Map elabParams = new HashMap();
          elabParams.put("user_id", user.getId());
          DataResult result = makeDataResult(params, elabParams, null, m);
          return result;
      }
  
      /**
       * Returns all of the errata in a channel
       * @param cid the channel id
       * @return all of the errata in the channel.
       */
      public static DataResult errataInChannel(Long cid) {
          SelectMode m = ModeFactory.getMode("Errata_queries", "channel_errata_for_list");
          Map params = new HashMap();
          params.put("cid", cid);
          DataResult dr = m.execute(params);
          return dr;
      }
  
  
      /**
       * Returns a list of ErrataOverview whose errata contains the packages
       * with the given pids.
       * @param pids list of package ids whose errata are sought.
       * @return a list of ErrataOverview whose errata contains the packages
       * with the given pids.
       */
      public static List<ErrataOverview> searchByPackageIds(List pids) {
          return ErrataFactory.searchByPackageIds(pids);
      }
  
      /**
       * Returns a list of ErrataOverview whose errata contains the packages
       * with the given pids.
       * @param pids list of package ids whose errata are sought.
       * @param org Organization to match results with
       * @return a list of ErrataOverview whose errata contains the packages
       * with the given pids.
       */
      public static List<ErrataOverview> searchByPackageIdsWithOrg(List pids, Org org) {
          return ErrataFactory.searchByPackageIdsWithOrg(pids, org);
      }
  
      /**
       * Returns a list of ErrataOverview matching the given errata ids.
       * @param eids Errata ids sought.
       * @param org Organization to match results with
       * @return a list of ErrataOverview matching the given errata ids.
       */
      public static List<ErrataOverview> search(List eids, Org org) {
          return ErrataFactory.search(eids, org);
      }
  
      /**
       * Returns the relevant errata.
       * @param user Currently logged in user.
       * @return relevant errata.
       */
      public static DataResult relevantErrata(User user) {
          SelectMode m = ModeFactory.getMode("Errata_queries", "relevant_errata");
  