All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.marvelution.jira.plugins.sonar.DefaultSonarPanelLayoutManagerImpl Maven / Gradle / Ivy

/*
 * Licensed to Marvelution under one or more contributor license 
 * agreements.  See the NOTICE file distributed with this work 
 * for additional information regarding copyright ownership.
 * Marvelution licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.marvelution.jira.plugins.sonar;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.marvelution.jira.plugins.sonar.service.SonarPanelLayoutManager;
import com.marvelution.jira.plugins.sonar.service.SonarPropertyManager;
import com.marvelution.jira.plugins.sonar.upgrade.SonarTabPanelLayoutUpgradeTask;
import com.opensymphony.module.propertyset.PropertySet;

/**
 * Default {@link SonarPanelLayoutManager} implementation
 * 
 * @author Mark Rekveld
 */
public class DefaultSonarPanelLayoutManagerImpl implements SonarPanelLayoutManager {

	private static final String CONFIG_NEXT_COLUMN_ID = "sonar.layout.next.columnId";

	private static final String CONFIG_LAYOUT_PREFIX = "sonar.layout.";

	private static final String CONFIG_LAYOUT_COLUMN_SUFFIX = ".columnId";

	private static final String CONFIG_LAYOUT_GADGETS_SUFFIX = ".gadgetIds";

	private final Logger logger = Logger.getLogger(DefaultSonarPanelLayoutManagerImpl.class);

	private final PropertySet propertySet;

	private final Map> layout = new HashMap>();

	/**
	 * Constructor
	 * 
	 * @param propertyManager the {@link SonarPropertyManager} implementation
	 */
	public DefaultSonarPanelLayoutManagerImpl(SonarPropertyManager propertyManager) {
		propertySet = propertyManager.getPropertySet();
		final Collection columnIds = new HashSet();
		final Collection keys = propertySet.getKeys(CONFIG_LAYOUT_PREFIX);
		for (Object entry : keys) {
			final String key = (String) entry;
			if (key.startsWith(CONFIG_LAYOUT_PREFIX) && key.endsWith(CONFIG_LAYOUT_COLUMN_SUFFIX)
				&& !CONFIG_NEXT_COLUMN_ID.equals(key)) {
				columnIds.add(propertySet.getInt(key));
			}
		}
		for (Integer columnId : columnIds) {
			load(columnId);
		}
		if (getNumberOfColumn() < 1) {
			// Something went wrong no columns are installed, add default columns from the upgrade task
			put(0, SonarTabPanelLayoutUpgradeTask.LEFT_COLUMN_GADGETS);
			put(0, SonarTabPanelLayoutUpgradeTask.RIGHT_COLUMN_GADGETS);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection getColumn(int columnId) {
		if (hasColumn(columnId)) {
			return Collections.unmodifiableCollection(layout.get(columnId));
		} else {
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Collection getColumnIds() {
		return Collections.unmodifiableCollection(layout.keySet());
	}

	/**
	 * {@inheritDoc}
	 */
	public Map> getLayout() {
		return Collections.unmodifiableMap(layout);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getNumberOfColumn() {
		return layout.size();
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean hasColumn(int columnId) {
		return layout.containsKey(columnId);
	}

	/**
	 * {@inheritDoc}
	 */
	public void put(int columnId, Collection gadgets) {
		store(columnId, gadgets);
	}

	/**
	 * {@inheritDoc}
	 */
	public void put(int columnId, String gadgetId) {
		validateGadgetId(gadgetId);
		put(columnId, Collections.singleton(gadgetId));
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(int columnId, Collection gadgetIds) {
		validateColumnId(columnId);
		if (gadgetIds == null) {
			throw new IllegalArgumentException("gadgetIds Collection may not be null");
		}
		logger.trace("Removing gadgets: " + StringUtils.join(gadgetIds, ',') + " from column: " + columnId);
		final Collection gadgets = layout.get(columnId);
		gadgets.removeAll(gadgetIds);
		store(columnId, gadgets);
		layout.put(columnId, gadgets);
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(int columnId, String gadgetId) {
		validateGadgetId(gadgetId);
		remove(columnId, Collections.singleton(gadgetId));
	}

	/**
	 * {@inheritDoc}
	 */
	public void remove(int columnId) {
		validateColumnId(columnId);
		logger.trace("Removing column: " + columnId);
		propertySet.remove(CONFIG_LAYOUT_PREFIX + columnId + CONFIG_LAYOUT_COLUMN_SUFFIX);
		propertySet.remove(CONFIG_LAYOUT_PREFIX + columnId + CONFIG_LAYOUT_GADGETS_SUFFIX);
		layout.remove(columnId);
	}

	/**
	 * Load the column with the given columnId from the store
	 * 
	 * @param columnId the columnId to load
	 */
	private void load(int columnId) {
		logger.trace("Loading column: " + columnId);
		final int column = propertySet.getInt(CONFIG_LAYOUT_PREFIX + columnId + CONFIG_LAYOUT_COLUMN_SUFFIX);
		final String gadgets = propertySet.getString(CONFIG_LAYOUT_PREFIX + columnId + CONFIG_LAYOUT_GADGETS_SUFFIX);
		final Collection gadgetIds = new HashSet();
		if (StringUtils.isNotBlank(gadgets)) {
			gadgetIds.addAll(Arrays.asList(StringUtils.split(gadgets, ',')));
		}
		layout.put(column, gadgetIds);
	}

	/**
	 * Store the columnId and associated gadgetIds
	 * 
	 * @param columnId the columnId to store
	 * @param gadgetIds the {@link Collection} gadgetIds to store
	 */
	private void store(int columnId, Collection gadgetIds) {
		int column = columnId;
		if (columnId < 1) {
			logger.trace("No columnId provided. Must be a new Column, get its Id");
			column = generateColumnId();
		}
		logger.trace("Storing column [" + columnId + "] with gadgets [" + StringUtils.join(gadgetIds, ',') + "]");
		propertySet.setInt(CONFIG_LAYOUT_PREFIX + column + CONFIG_LAYOUT_COLUMN_SUFFIX, column);
		propertySet.setString(CONFIG_LAYOUT_PREFIX + column + CONFIG_LAYOUT_GADGETS_SUFFIX, StringUtils.join(
			gadgetIds, ','));
		layout.put(column, gadgetIds);
	}

	/**
	 * Generate the next column Id
	 * 
	 * @return the Id for the next column
	 */
	private int generateColumnId() {
		int nextId = 1;
		if (propertySet.exists(CONFIG_NEXT_COLUMN_ID)) {
			nextId = propertySet.getInt(CONFIG_NEXT_COLUMN_ID);
		}
		propertySet.setInt(CONFIG_NEXT_COLUMN_ID, nextId + 1);
		return nextId;
	}

	/**
	 * Validate the given columnId
	 * 
	 * @param columnId the columnId to validate
	 */
	private void validateColumnId(int columnId) {
		if (columnId < 1) {
			throw new IllegalArgumentException("columnId may not be smaller then 1");
		}
	}

	/**
	 * Validate the given gadgetId
	 * 
	 * @param gadgetId the gadgetId to validate
	 */
	private void validateGadgetId(String gadgetId) {
		if (StringUtils.isBlank(gadgetId)) {
			throw new IllegalArgumentException("gadgetId may not be empty or null");
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy