/**
   * Adds activation information for the given profile.
   *
   * @param mavenProfileDescriptor The profile descriptor.
   * @param activation The activation information.
   * @param store The database.
   */
  private void addActivation(
      MavenProfileDescriptor mavenProfileDescriptor, Activation activation, Store store) {
    if (null == activation) {
      return;
    }
    MavenProfileActivationDescriptor profileActivationDescriptor =
        store.create(MavenProfileActivationDescriptor.class);
    mavenProfileDescriptor.setActivation(profileActivationDescriptor);

    profileActivationDescriptor.setJdk(activation.getJdk());
    profileActivationDescriptor.setActiveByDefault(activation.isActiveByDefault());

    ActivationFile activationFile = activation.getFile();
    if (null != activationFile) {
      MavenActivationFileDescriptor activationFileDescriptor =
          store.create(MavenActivationFileDescriptor.class);
      profileActivationDescriptor.setActivationFile(activationFileDescriptor);
      activationFileDescriptor.setExists(activationFile.getExists());
      activationFileDescriptor.setMissing(activationFile.getMissing());
    }
    ActivationOS os = activation.getOs();
    if (null != os) {
      MavenActivationOSDescriptor osDescriptor = store.create(MavenActivationOSDescriptor.class);
      profileActivationDescriptor.setActivationOS(osDescriptor);
      osDescriptor.setArch(os.getArch());
      osDescriptor.setFamily(os.getFamily());
      osDescriptor.setName(os.getName());
      osDescriptor.setVersion(os.getVersion());
    }
    ActivationProperty property = activation.getProperty();
    if (null != property) {
      PropertyDescriptor propertyDescriptor = store.create(PropertyDescriptor.class);
      profileActivationDescriptor.setProperty(propertyDescriptor);
      propertyDescriptor.setName(property.getName());
      propertyDescriptor.setValue(property.getValue());
    }
  }
  public static ProfileApplicationResult applyProfiles(
      MavenModel model,
      File basedir,
      Collection<String> explicitProfiles,
      Collection<String> alwaysOnProfiles)
      throws RemoteException {
    Model nativeModel = MavenModelConverter.toNativeModel(model);

    List<Profile> activatedPom = new ArrayList<Profile>();
    List<Profile> activatedExternal = new ArrayList<Profile>();
    List<Profile> activeByDefault = new ArrayList<Profile>();

    List<Profile> rawProfiles = nativeModel.getProfiles();
    List<Profile> expandedProfilesCache = null;

    for (int i = 0; i < rawProfiles.size(); i++) {
      Profile eachRawProfile = rawProfiles.get(i);

      boolean shouldAdd =
          explicitProfiles.contains(eachRawProfile.getId())
              || alwaysOnProfiles.contains(eachRawProfile.getId());

      Activation activation = eachRawProfile.getActivation();
      if (activation != null) {
        if (activation.isActiveByDefault()) {
          activeByDefault.add(eachRawProfile);
        }

        // expand only if necessary
        if (expandedProfilesCache == null)
          expandedProfilesCache = doInterpolate(nativeModel, basedir).getProfiles();
        Profile eachExpandedProfile = expandedProfilesCache.get(i);

        for (ProfileActivator eachActivator : getProfileActivators(basedir)) {
          try {
            if (eachActivator.canDetermineActivation(eachExpandedProfile)
                && eachActivator.isActive(eachExpandedProfile)) {
              shouldAdd = true;
              break;
            }
          } catch (ProfileActivationException e) {
            Maven3ServerGlobals.getLogger().warn(e);
          }
        }
      }

      if (shouldAdd) {
        if (MavenConstants.PROFILE_FROM_POM.equals(eachRawProfile.getSource())) {
          activatedPom.add(eachRawProfile);
        } else {
          activatedExternal.add(eachRawProfile);
        }
      }
    }

    List<Profile> activatedProfiles =
        new ArrayList<Profile>(activatedPom.isEmpty() ? activeByDefault : activatedPom);
    activatedProfiles.addAll(activatedExternal);

    for (Profile each : activatedProfiles) {
      new DefaultProfileInjector().injectProfile(nativeModel, each, null, null);
    }

    return new ProfileApplicationResult(
        MavenModelConverter.convertModel(nativeModel, null), collectProfilesIds(activatedProfiles));
  }