# This file is part of Superwas.
# 
# Superwas is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Superwas is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Superwas.  If not, see <http://www.gnu.org/licenses/>.

# Application Classes
# 
# Author: Andre van Dijk (SuperClass IT)
# Date: $Date: 2013-01-04 16:04:36 +0100 (vr, 04 jan 2013) $
# $Id: application.py 424 2013-01-04 15:04:36Z andre $
class TaskInfoData:
	def __init__(self, taskInfo):
		self.__taskInfo=taskInfo

	def getKey(self, key):
		if self.__taskInfo.has_key(key):
			return self.__taskInfo[key]
		else:
			logger.debug("Taskinfo key %s missing returning empty" % key)
			return ""

class WebServerTarget(WASConfig):
	level=6
	def __init__(self, parent=None):
		WASConfig.__init__(self, parent)
		self.validParents=["Application"]
		self.ref_attributes={
			'webServer':None
		}

	def setWebServer(self, webServer):
		if webServer is not None and isinstance(webServer, WebServer):
			self.ref_attributes['webServer']=webServer
		else:
			raise Exception("Invalid WebServer")
		self.logValue()
		
	def getWebServer(self):
		return self.ref_attributes['webServer']

	def getWebServerType(self):
		return "WebServer"

	def getConfigType(self):
		return ""

	def setConfigID(self):
		pass

	def getContainmentPath(self):
		return ""

	def create(self):
		pass
	def remove(self):
		pass

class Role(WASConfig):
	level=6
	def __init__(self, parent=None):
		WASConfig.__init__(self, parent)
		self.validParents=["Application"]
		self.opt_attributes={
			'user':'',
			'group':'',
			'everyone':'false',
			'allauthuser':'false'
		}

	def setUser(self, user):
		if user is not None: self.opt_attributes['user']=user
		self.logValue()
	def getUser(self):
		return self.opt_attributes['user']

	def setGroup(self, group):
		if group is not None: self.opt_attributes['group']=group
		self.logValue()
	def getGroup(self):
		return self.opt_attributes['group']

	def setEveryone(self, everyone):
		if everyone is not None: self.opt_attributes['everyone']=everyone
		self.logValue()
	def getEveryone(self):
		return self.opt_attributes['everyone']

	def setAllauthuser(self, allauthuser):
		if allauthuser is not None: self.opt_attributes['allauthuser']=allauthuser
		self.logValue()
	def getAllauthuser(self):
		return self.opt_attributes['allauthuser']

	def getOption(self):
		return "-MapRolesToUsers"

	def getMapping(self):
		roleMapping=[[self.getName(),self.getEveryone(),self.getAllauthuser(),self.getUser(),self.getGroup()]]
		logger.debug("Role mapping : %s" % roleMapping)
		return roleMapping

	def getConfigType(self):
		return ""

	def setConfigID(self):
		pass

	def getContainmentPath(self):
		return ""
	def create(self):
		pass
	def remove(self):
		pass

class WebModule(WASConfig):
	level=6
	def __init__(self, parent=None):
		WASConfig.__init__(self, parent)
		self.validParents=["Application"]
		self.opt_attributes={
			'contextRoot':'',
			'classLoaderMode':'PARENT_LAST',
		}
		self.ref_attributes={
			'virtualHost':None
		}

	def setContextRoot(self, contextRoot):
		if contextRoot is not None:
			self.opt_attributes['contextRoot']=contextRoot
		self.logVal
ue()
	def getContextRoot(se
lf):
		return self.opt_attributes['contextRoot']

	def setClassLoaderMode(self, classLoaderMode):
		if classLoaderMode is not None:
			self.opt_attributes['classLoaderMode']=classLoaderMode
		self.logValue()
	def getClassLoaderMode(self):
		return self.opt_attributes['classLoaderMode']

	def setVirtualHost(self, virtualHost):
		"""
		Set the VirtualHost to use in application deployement. VirtualHost
		should be a VirtualHost object otherwise an exception is
		raised.
		"""
		if isinstance(virtualHost, VirtualHost):
			self.ref_attributes['virtualHost']=virtualHost
		else:
			raise Exception("VirtualHost parameter is not a VirtualHost object.")
		self.logValue()
	def getVirtualHost(self):
		return self.ref_attributes['virtualHost']
	def getVirtualHostType(self):
		return "VirtualHost"

	def getMapping(self):
		ctxrootattr=[]
		vhostattr=[]
		taskInfoWebMods=self.getParent().getTaskInfoData("MapWebModToVH")
		for tiwm in taskInfoWebMods:
			wmuri=tiwm.getKey('uri')
			wmwar=wmuri.split(',')[0]
			wmname=tiwm.getKey('web module')
			if self.getVirtualHost()!=None:
				vhostattr.append([wmname, wmuri, self.getVirtualHost().getName()])
			if self.getContextRoot()!="":
				if self.getName()==wmname:
					ctxrootattr.append([wmname, wmuri, self.getContextRoot()])
			logger.debug("Virtual host mappings: %s" % vhostattr)
		return [vhostattr, ctxrootattr]

	def getConfigType(self):
		return ""

	def setConfigID(self):
		pass

	def getContainmentPath(self):
		return ""
	def create(self):
		pass
	def remove(self):
		pass

class BaseResourceRef(WASConfig):
	level=6
	def __init__(self, parent=None):
		WASConfig.__init__(self, parent)
		self.validParents=["Application"]
		self.man_attributes={
			'module':"",
			'uri':"",
			'jndi':""
		}
		self.opt_attributes={
			'ejb':""
		}

	def setModule(self, module):
		if module is not None:
			self.man_attributes['module']=module
		self.logValue()
	def getModule(self):
		return self.man_attributes['module']

	def setEjb(self, ejb):
		if ejb is not None:
			self.opt_attributes['ejb']=ejb
		self.logValue()
	def getEjb(self):
		return self.opt_attributes['ejb']

	def setJndi(self, jndi):
		if jndi is not None:
			self.man_attributes['jndi']=jndi
		self.logValue()
	def getJndi(self):
		return self.man_attributes['jndi']

	def setUri(self, uri):
		if uri is not None:
			self.man_attributes['uri']=uri
		self.logValue()
	def getUri(self):
		return self.man_attributes['uri']

	def getOption(self):
		return ""

	def getMapping(self):
		return ""

	def getConfigType(self):
		return ""

	def setConfigID(self):
		pass

	def getContainmentPath(self):
		return ""

	def getKeyAttribute(self):
		return "module"
	def create(self):
		pass
	def remove(self):
		pass

class BeanRef(BaseResourceRef):
	def __init__(self, parent=None):
		BaseResourceRef.__init__(self, parent)

	def getOption(self):
		return "-BindJndiForEJBNonMessageBinding"

	def getMapping(self):
		mapping=[]
		for taskInfo in self.getParent().getTaskInfoData("BindJndiForEJBNonMessageBinding"):
			refModule = taskInfo.getKey("ejb module")
			refEjb = taskInfo.getKey("ejb")
			refUri = taskInfoData.getKey("uri")
			if refmodule == self.getModule() and refEjb == self.getEjb() and refUri==self.getUri():
				reftoref = [refModule , refEjb , refUri , self.getJndi()]
				logger.debug("Bind JNDI rule for non Msg EJB : %s" %  reftoref)
				mapping.append(reftoref)	
		return mapping

class CmpRef(BaseResourceRef):
	def __init__(self, parent=None):
		BaseResourceRef.__init__(self, parent)

	def getOption(self):
		return "-DataSourceFor20CMPBeans"

	def getMapping(self):
		mapping=[]
		for taskInfo in self.getParent().getTaskInfoData("DataSourceFor20CMPBeans"):
			refModule = taskInfo.getKey("ejb module")
			refEjb = taskInfo.getKey("ejb")
			refUri = taskInfoData.getKey("uri")
			resourceAuth='cmpBinding.container'

			if refModule==self.getModule() and refEjb==self.getEjb() and uri==self.getUri():
				reftoref=[ refModule ,refEjb , refUri , self.getJndi(), resourceAuth, "", "" ]
				logger.debug( "DataSourceFor20CMPBeans rule : %s" %  reftoref )
				mapping.append(reftoref)
		return mapping

class BaseReferenceResourceRef(BaseResourceRef):
	def __init__(self, parent=None):
		BaseResourceRef.__init__(self, parent)
		self.man_attributes['referenceBinding']=""

	def setReferenceBinding(self, referenceBinding):
		if referenceBinding is not None:
			self.man_attributes['referenceBinding']=referenceBinding
		self.logValue()
	def getReferenceBinding(self):
		return self.man_attributes['referenceBinding']

class ResRef(BaseRe