/*
 * The MIT License
 * 
 * Copyright (c) 2011, Jesse Farinacci
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jenkinsci.plugins.managegroup;

import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.JobPropertyDescriptor;
import hudson.model.Job;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;

import java.util.Set;
import java.util.TreeSet;

import javax.inject.Inject;

import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

@Extension
public class ProjectGroupAssociationDescriptor extends JobPropertyDescriptor {
	private String groupName;

	@Inject
	private transient GroupsGlobalConfiguration globalGroup;

	public ProjectGroupAssociationDescriptor() {
		super(ProjectGroupAssociation.class);
		load();
	}

	@DataBoundConstructor
	public ProjectGroupAssociationDescriptor(String groupName) {
		setGroupName(groupName);
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	@Override
	public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends Job> jobType) {
		return true;
	}

	@Override
	public String getDisplayName() {
		return "GroupName";
	}

	@Override
	public ProjectGroupAssociation newInstance(StaplerRequest req, JSONObject formData) throws FormException {
		String grp = null;
		if (formData != null && !formData.isNullObject()) {
			grp = formData.getString("groupName");
		}

		if (grp == null || grp.trim().length() == 0) {
			throw new FormException("Error group empty", "groupName");
		}

		ProjectGroupAssociation property = new ProjectGroupAssociation(grp);
		return property;
	}

	public FormValidation doCheckGroupName(
			@QueryParameter(value = "groupName", fixEmpty = true, required = true) String grp) {
		if (grp == null) {
			return FormValidation.warning("Group must be set");

		}
		return FormValidation.ok();
	}

	public ListBoxModel doFillGroupNameItems() {
		ListBoxModel items = new ListBoxModel();

		Set<String> groups = new TreeSet<String>();
		for (GroupProvider p : ExtensionList.lookup(GroupProvider.class)) {
			groups.addAll(p.groupFor(Jenkins.getAuthentication().getName()));
		}
		groups.addAll(globalGroup.getGroups());

		for (String group : groups) {
			Option o = new Option(group, group, (group.equals(groupName)));
			items.add(o);
		}

		return items;
	}

	@Override
	public boolean configure(StaplerRequest req, JSONObject formData) {
		// req.bindJSON(this, formData);
		save();
		return true;
	}
}

@@ -1301,19 +1301,83 @@
 n.model.
-Job
+TopLevelItem;%0Aimport hudson.model.Job;%0Aimport hudson.model.ListView
 ;%0Aimport
@@ -1479,16 +1479,44 @@
 ption;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -1643,24 +1643,61 @@
 SONObject;%0A%0A
+import org.kohsuke.stapler.Ancestor;%0A
 import org.k
@@ -2982,26 +2982,574 @@
 p);%0A
-%09%09return property;
+%0A%09%09// Remove the project direct association if view is a group view%0A%09%09fixProjectAssociation(req);%0A%0A%09%09return property;%0A%09%7D%0A%0A%09private void fixProjectAssociation(StaplerRequest req) %7B%0A%09%09Ancestor viewAncestor = req.findAncestor(ListView.class);%0A%09%09if (viewAncestor == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09ListView view = (ListView) viewAncestor.getObject();%0A%09%09Ancestor projectAncestor = req.findAncestor(TopLevelItem.class);%0A%09%09if (projectAncestor == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09view.remove((TopLevelItem) projectAncestor.getObject());%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09// NO OP%0A%09%09%7D
 %0A%09%7D%0A

Remove project from current view