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

com.quamto.jira.base.BaseController Maven / Gradle / Ivy

Go to download

Library that contains the class base to support functionalities in the JIRA Plugins

The newest version!
package com.quamto.jira.base;

import com.atlassian.connect.spring.AtlassianHost;
import com.atlassian.connect.spring.AtlassianHostRestClients;
import com.atlassian.connect.spring.AtlassianHostUser;
import com.quamto.core.Result;
import com.quamto.jira.api.config.SecurityManager;
import com.quamto.jira.business.config.AddOnSettingsBusiness;
import com.quamto.jira.business.config.model.AddOnSettingsModel;
import com.quamto.jira.business.context.EnvironmentVariables;
import com.quamto.jira.business.time.setup.SetupTimeTrack;
import com.quamto.jira.data.common.JEnumerators;
import com.quamto.jira.data.config.entity.AddOnSettingsEntity;
import com.quamto.jira.models.config.JiraStatus;
import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

/**
 * Class that contains the base methods for the controllers classes
 * 
 * @author jholguin
 * @since 19/06/2016
 *
 */
public class BaseController {

	@Autowired
	private AtlassianHostRestClients atlassianHostRestClients;

	protected String className = "";
	protected Logger logger = Logger.getLogger(BaseController.class);

	public BaseController(Class subClass) {
		this(subClass.getName());
	}
	
	public BaseController(String subClassName) {
		className = subClassName;
	}
	
	/**
	 * Gets the client identifier stored in the database
	 * @param hostUser Host user information to get the client identifier
	 * @return Client identifier from the local storage
	 */
	public Long getClientIdentifier(AtlassianHostUser hostUser) throws Exception{
		Long clientId = 0L;
		try {
			Result result = getAddOnSettings(hostUser);
			if(result.isSuccessful()){
				AddOnSettingsModel addOnSettingsModel = (AddOnSettingsModel) result.getPayload();
				if(addOnSettingsModel != null){
					clientId = addOnSettingsModel.getId();
				}
				if(clientId<=0 || clientId == null){
					throw new Exception("Invalid internal client identifier");
				}
			}else{
				throw new Exception(result.getException().getMessage());
			}
		} catch (Exception e) {
			throw e;
		}
		return clientId;
	}
	
	/**
	 * Gets the add-on settings information from client key 
	 * @param hostUser Host user information
	 * @return
	 */
	public Result getAddOnSettings(AtlassianHostUser hostUser) throws Exception{
		Result result = new Result();
		try {
			if(hostUser != null){
				AtlassianHost atlassianHost = hostUser.getHost();
				try(AddOnSettingsBusiness addOnBU = new AddOnSettingsBusiness()){
					result = addOnBU.getByClientKey(atlassianHost.getClientKey());
					
					if(result.isSuccessful()){
						DozerBeanMapper mapper = new DozerBeanMapper();
						AddOnSettingsModel addOnSettingsModel =  null;
						AddOnSettingsEntity addOnSettingsEntity = (AddOnSettingsEntity) result.getPayload();
						//Validate if is the first configuration
						if(addOnSettingsEntity.getFirstConfiguration() == JEnumerators.YesNo.No){
							Result resultSetup = setupFirstConfiguration(addOnSettingsEntity);
							if(resultSetup.isSuccessful()){
								addOnSettingsEntity.setFirstConfiguration(JEnumerators.YesNo.Yes);
								addOnBU.update(addOnSettingsEntity);
							}else{
								logger.info("Failed first configuration with client: " + addOnSettingsEntity.getID().toString());
							}
						}
						addOnSettingsModel = mapper.map(addOnSettingsEntity, AddOnSettingsModel.class);
					
						result.setPayload(addOnSettingsModel);
					}
				}
			}
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	/**
	 * Setup example data to the plugin component
	 * @return
	 */
	public Result setupFirstConfiguration(AddOnSettingsEntity addOnSettingsEntity){
		Result result = new Result();
		logger.info("First configuration for: " + addOnSettingsEntity.getClientKey());
		try{
			if(EnvironmentVariables.getAddonKey() == EnvironmentVariables.ADD_ON_KEY_TIMETRACK){
				JiraStatus[] jiraStatuses = atlassianHostRestClients.authenticatedAsAddon().getForObject("/rest/api/2/status", JiraStatus[].class);
				logger.info("Total of jira status for first Time track configuration: " + jiraStatuses.length);
				try(SetupTimeTrack setupTimeTrack = new SetupTimeTrack()){
					result = setupTimeTrack.SetupFirstTime(addOnSettingsEntity, jiraStatuses);
				}
			}
		}catch (Exception e){
			result = new Result(e);
		}
		return result;
	}

	/**
	 * Validates Session status
	 * @param request Http request
	 * @return ResultadoOPR with the validation
	 */
	public Result validateRequestPrivileges(HttpServletRequest request){
		Result result = new Result();
		try {
			result = SecurityManager.validateRequestPrivileges(request);
		} catch (Exception e) {
			result = new Result(e, className + "-validateRequestPrivileges");
		}
		return result;
	}

	/**
	 * Translate a result operation
	 * @param result Result that contains the operation results
	 * @return result translated
	 */
	public Result translateResult(Result result){
		if(result != null){
			return result.translateLocaleMessages();
		}else{
			return null;
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy