ApiApplicationBean.java 3.53 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.beans;

import java.util.Calendar;
import java.util.List;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;

import fi.codecrew.moya.enums.apps.SpecialPermission;
import fi.codecrew.moya.facade.ApiApplicationFacade;
import fi.codecrew.moya.facade.ApiApplicationInstanceFacade;
import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.model.ApiApplication;
import fi.codecrew.moya.model.ApiApplicationInstance;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.utilities.PasswordFunctions;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventType;

/**
 * Session Bean implementation class RestAuthBean
 */
@Singleton
@LocalBean
@DeclareRoles(SpecialPermission.S_SUPERADMIN)
public class ApiApplicationBean implements ApiApplicationBeanLocal {

	@EJB
	ApiApplicationFacade applicationFacade;

	@EJB
	PermissionBean permissionBean;

	@EJB
	ApiApplicationInstanceFacade instanceFacade;

	@EJB
	private EventUserFacade eventUserFacade;
	@EJB
	EventBean eventBean;

	@EJB
	LoggingBeanLocal loggingBean;

	@EJB
	UserBean userBean;

	/**
	 * Default constructor.
	 */
	public ApiApplicationBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	@RolesAllowed(SpecialPermission.S_USER)
	public ApiApplicationInstance createApplicationInstance(ApiApplication application) {

		// ugly as shit sanitation for eventName, sorry
		String eventName = eventBean.getCurrentEvent().getName().replace(" ", "_").replace("ä", "a").replace("ö", "o")
				.replace("Ä", "A").replace("Ö", "O").replace("å", "a").replace("Å", "A");

		String authname = permissionBean.getCurrentUser().getLogin() + "_" + application.getName() + "_" + eventName;

		while (instanceFacade.findInstance(application, authname) != null) {
			authname += "_";
		}

		ApiApplicationInstance instance = new ApiApplicationInstance();

		instance.setApplication(application);
		instance.setAuthname(authname);
		instance.setName(application.getName() + " for user: " + permissionBean.getCurrentUser().getLogin());
		instance.setCreated(Calendar.getInstance().getTime());
		instance.setEnabled(true);
		instance.setEventuser(permissionBean.getCurrentUser());
		instance.setSecretKey(PasswordFunctions.generateRandomString(30));
		instanceFacade.create(instance);

		loggingBean.sendMessage(MoyaEventType.APPLICATION_INSTANCE_CREATED,
				"New applicationinstance created for software: ", application);

		return instance;
	}

	@Override
	@RolesAllowed(SpecialPermission.S_USER)
	public List<ApiApplication> findMyApplications() {
		EventUser curruser = permissionBean.getCurrentUser();
		return applicationFacade.findForUser(curruser);
	}

	@Override
	@RolesAllowed(SpecialPermission.S_SUPERADMIN)
	public List<ApiApplication> findAllApplications() {
		return applicationFacade.findAll();
	}

}