/**
  * Fetch the log entries as an atom feed. May trigger a purge of old messages, according to this
  * log service's purge policy. Includes paging options.
  *
  * @param maxResults maximum number of log results
  * @param startIndex starting index of log results
  * @return a {@code Feed} containing all the entries in the log
  */
 public synchronized Feed getFeed(Integer maxResults, Integer startIndex) {
   Feed feed = new Feed();
   feed.setTitle(
       "Plugin management log for "
           + applicationProperties.getDisplayName()
           + " ("
           + applicationProperties.getBaseUrl()
           + ")");
   feed.setModified(lastModified);
   addLink(feed, applicationProperties.getBaseUrl(), "base");
   addAuditLogEntries(feed, maxResults, startIndex);
   return feed;
 }
  OAuthService createOAuthScribeService() {
    // param "t" is holding information where to redirect from "wainting screen"
    // (AddBitbucketOrganization, AddGithubOrganization ...)
    String redirectBackUrl =
        new StringBuilder()
            .append(ap.getBaseUrl())
            .append("/secure/admin/AddOrganizationProgressAction!default.jspa?organization=")
            .append(organization)
            .append("&autoLinking=")
            .append(getAutoLinking())
            .append("&url=")
            .append(url)
            .append("&autoSmartCommits=")
            .append(getAutoSmartCommits())
            .append("&atl_token=")
            .append(getXsrfToken())
            .append("&t=1")
            .append(getSourceAsUrlParam())
            .toString();

    return createBitbucketOAuthScribeService(redirectBackUrl);
  }
 /** Get context path. */
 public String getBaseUrl() {
   return applicationProperties.getBaseUrl();
 }
  @Override
  public Map<String, Object> getVelocityParameters(
      Issue issue, CustomField field, FieldLayoutItem fieldLayoutItem) {
    Map<String, Object> params = super.getVelocityParameters(issue, field, fieldLayoutItem);
    params.put("i18n", getI18nBean());
    params.put("baseUrl", applicationProperties.getBaseUrl());

    Long prId;
    if (field.isAllProjects()) {
      prId = Consts.PROJECT_ID_FOR_GLOBAL_CF;
    } else {
      if (issue == null) {
        return params;
      }
      prId = issue.getProjectObject().getId();
    }

    String jqlData = qfMgr.getQueryFieldData(field.getIdAsLong(), prId);
    boolean addNull = qfMgr.getAddNull(field.getIdAsLong(), prId);
    boolean isAutocompleteView = qfMgr.isAutocompleteView(field.getIdAsLong(), prId);
    List<String> options = qfMgr.getLinkeFieldsOptions(field.getIdAsLong(), prId);

    params.put("isAutocompleteView", isAutocompleteView);
    params.put("prId", prId.toString());

    String cfValue = field.getValueFromIssue(issue);
    if (Utils.isValidStr(cfValue)) {
      MutableIssue mi = issueMgr.getIssueObject(cfValue);
      if (mi != null && Utils.isValidStr(mi.getSummary())) {
        StringBuilder sb = new StringBuilder();
        if (options.contains("status")) {
          sb.append(getI18nBean().getText("queryfields.opt.status"))
              .append(": ")
              .append(mi.getStatusObject().getName());
        }
        if (options.contains("assignee") && mi.getAssigneeUser() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          User aUser = mi.getAssigneeUser();
          String encodedUser;
          try {
            encodedUser = URLEncoder.encode(aUser.getName(), "UTF-8");
          } catch (UnsupportedEncodingException e) {
            // --> impossible
            encodedUser = aUser.getName();
          }

          sb.append(getI18nBean().getText("queryfields.opt.assignee"))
              .append(": ")
              .append("<a class='user-hover' rel='")
              .append(aUser.getName())
              .append("' id='issue_summary_assignee_'")
              .append(aUser.getName())
              .append("' href='/secure/ViewProfile.jspa?name='")
              .append(encodedUser)
              .append("'>")
              .append(aUser.getDisplayName())
              .append("</a>");
        }
        if (options.contains("priority") && mi.getPriorityObject() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.priority"))
              .append(": ")
              .append(mi.getPriorityObject().getName());
        }
        if (options.contains("due") && mi.getDueDate() != null) {
          if (sb.length() > 0) {
            sb.append(", ");
          }
          sb.append(getI18nBean().getText("queryfields.opt.due"))
              .append(": ")
              .append(
                  ComponentAccessor.getJiraAuthenticationContext()
                      .getOutlookDate()
                      .format(mi.getDueDate()));
        }

        if (sb.length() > 0) {
          sb.insert(0, " (");
          sb.append(")");
        }

        IssueData issueData;
        if (options.contains("justDesc")) {
          String descr = mi.getDescription();
          if (Utils.isValidStr(descr)) {
            issueData = new IssueData(descr, sb.toString());
          } else {
            issueData = new IssueData(mi.getSummary(), sb.toString());
          }
        } else if (options.contains("key")) {
          issueData = new IssueData(mi.getKey().concat(":").concat(mi.getSummary()), sb.toString());
        } else {
          issueData = new IssueData(mi.getSummary(), sb.toString());
        }
        params.put("fullValue", issueData);
      }
    }

    if (!Utils.isValidStr(jqlData)) {
      params.put("jqlNotSet", Boolean.TRUE);
      return params;
    }
    params.put("jqlNotSet", Boolean.FALSE);
    params.put("options", options);

    if (options.contains("editKey")) {
      params.put("hasKey", Boolean.TRUE);
    }

    User user = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();
    SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlData);
    if (parseResult.isValid()) {
      params.put("jqlNotValid", Boolean.FALSE);
      Query query = parseResult.getQuery();
      try {
        Map<String, String> cfVals = new LinkedHashMap<String, String>();
        SearchResults results = searchService.search(user, query, PagerFilter.getUnlimitedFilter());
        List<Issue> issues = results.getIssues();
        for (Issue i : issues) {
          String summary;
          if (options.contains("justDesc")) {
            String descr = i.getDescription();
            if (Utils.isValidStr(descr)) {
              summary = descr;
            } else {
              summary = i.getSummary();
            }
          } else if (options.contains("editKey")) {
            summary = i.getKey().concat(":").concat(i.getSummary());
          } else {
            summary = i.getSummary();
          }
          cfVals.put(i.getKey(), summary);
        }

        if (addNull) {
          cfVals.put("Empty", Consts.EMPTY_VALUE);
        }

        String selected = Consts.EMPTY_VALUE;
        String value = (String) issue.getCustomFieldValue(field);
        for (Map.Entry<String, String> cf : cfVals.entrySet()) {
          if (value != null && cf.getKey().equals(value)) {
            selected = value;
            break;
          }
        }

        if (isAutocompleteView) {
          Issue selectedIssue = issueMgr.getIssueObject(selected);
          if (selectedIssue != null) {
            params.put("selIssue", selectedIssue);
          }
        } else {
          if (selected.equals("")) {
            String defaultValue = (String) field.getDefaultValue(issue);
            if (defaultValue != null
                && defaultValue.length() > 0
                && cfVals.keySet().contains(defaultValue)) {
              selected = defaultValue;
            }
          }

          if (cfVals != null && !cfVals.isEmpty() && selected.equals("")) {
            selected = cfVals.keySet().iterator().next();
          }
        }

        params.put("selected", selected);
        params.put("isError", Boolean.FALSE);
        params.put("cfVals", cfVals);
      } catch (SearchException e) {
        params.put("isError", Boolean.TRUE);
      }
    } else {
      params.put("jqlNotValid", Boolean.TRUE);
      return params;
    }

    return params;
  }
  private GadgetSpec getGadgetSpec(
      final URI specUri,
      final Map<String, String> userPrefs,
      final GadgetRequestContext gadgetRequestContext)
      throws GadgetParsingException {
    final URI absoluteSpecUri =
        Uri.resolveUriAgainstBase(applicationProperties.getBaseUrl(), specUri);
    GadgetContext gadgetContext =
        new GadgetContext() {
          @Override
          public URI getUrl() {
            return absoluteSpecUri;
          }

          @Override
          public boolean getIgnoreCache() {
            return gadgetRequestContext.getIgnoreCache();
          }

          @Override
          public RenderingContext getRenderingContext() {
            return RenderingContext.CONTAINER;
          }

          @Override
          public UserPrefs getUserPrefs() {
            return new UserPrefs(userPrefs);
          }

          @Override
          public Locale getLocale() {
            if (gadgetRequestContext.getLocale() != null) {
              return gadgetRequestContext.getLocale();
            }
            return new Locale("");
          }

          @Override
          public boolean getDebug() {
            return gadgetRequestContext.isDebuggingEnabled();
          }
        };

    try {
      org.apache.shindig.gadgets.spec.GadgetSpec shindigGadgetSpec =
          substituter.substitute(gadgetContext, shindigFactory.getGadgetSpec(gadgetContext));
      ModulePrefs prefs = shindigGadgetSpec.getModulePrefs();

      return GadgetSpec.gadgetSpec(specUri)
          .userPrefs(transform(shindigGadgetSpec.getUserPrefs(), UserPrefToUserPrefSpec.FUNCTION))
          .viewsNames(shindigGadgetSpec.getViews().keySet())
          .scrolling(prefs.getScrolling())
          .height(prefs.getHeight())
          .width(prefs.getWidth())
          .title(prefs.getTitle())
          .titleUrl(nullSafeToJavaUri(prefs.getTitleUrl()))
          .thumbnail(nullSafeToJavaUri(prefs.getThumbnail()))
          .author(prefs.getAuthor())
          .authorEmail(prefs.getAuthorEmail())
          .description(prefs.getDescription())
          .directoryTitle(prefs.getDirectoryTitle())
          .features(
              unmodifiableMap(
                  transformValues(prefs.getFeatures(), ShindigFeatureToFeature.FUNCTION)))
          .unsupportedFeatureNames(getUnsupportedFeatureNames(prefs))
          .build();
    } catch (GadgetException e) {
      throw new GadgetParsingException(e);
    }
  }