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

com.jk.application.AbstractApplication Maven / Gradle / Ivy

/*
 * Copyright 2002-2016 Jalal Kiswani.
 *
 * Licensed 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.jk.application;

import java.util.ArrayList;
import java.util.List;

import com.jk.exceptions.JKNotAllowedOperationException;
import com.jk.exceptions.handler.JKExceptionUtil;
import com.jk.locale.JKLocale;
import com.jk.metadata.application.api.Application;
import com.jk.metadata.application.api.Menu;
import com.jk.metadata.application.api.MenuItem;
import com.jk.metadata.application.api.Module;
import com.jk.metadata.application.exceptions.ApplicationException;
import com.jk.metadata.application.listener.ApplicationListener;
import com.jk.metadata.application.ui.UIApplicationFrame;
import com.jk.security.JKPrivilige;
import com.jk.security.JKSecurityManager;
import com.jk.util.JK;
import com.jk.util.UserPreferences;

/**
 * The Class Application.
 *
 * @author Jalal Kiswani
 */
public abstract class AbstractApplication implements Application {
	private int autoLogoutInterval = 15;
	String applicationName;

	List modules;
	List listeners = new ArrayList();

	private String configFileName;

	// MainMenu mainMenu=new MainMenu();

	String splashImage;

	String homeImage;

	private int applicationId = 8643;// just dummy value

	private UIApplicationFrame applicationFrame;

	private boolean viewModules = true;
	private String backgroundImage;
	private JKLocale locale;

	// /**
	// * @return the mainMenu
	// */
	// public MainMenu getMainMenu() {
	// return mainMenu;
	// }

	/**
	 * Adds the listener.
	 *
	 * @param listener
	 *            the listener
	 */
	public void addListener(final ApplicationListener listener) {
		this.listeners.add(listener);
	}

	/**
	 *
	 * @param priviligeId
	 * @param priviligeName
	 * @param label
	 * @throws SecurityException
	 */
	private String checkValidPrivilige(final int priviligeId, final String priviligeName, final String label, final String parentPriilige)
			throws SecurityException {
		final StringBuffer buf = new StringBuffer();
		buf.append("INSERT INTO sec_privileges values (");
		buf.append(priviligeId);
		buf.append(",'");
		buf.append(priviligeName);
		buf.append("','");
		buf.append(label);
		buf.append("',");
		buf.append(parentPriilige);
		buf.append(");\n");
		return buf.toString();
	}

	/**
	 * Find menu item.
	 *
	 * @param menuItemName
	 *            the menu item name
	 * @return the menu item
	 * @throws JKNotAllowedOperationException
	 *             the JK not allowed operation exception
	 * @throws SecurityException
	 *             the security exception
	 */
	public MenuItem findMenuItem(final String menuItemName) throws JKNotAllowedOperationException, SecurityException {
		for (final Module module : getModules()) {
			for (final Menu menu : module.getMenu()) {
				for (final MenuItem item : menu.getItems()) {
					if (item.getName().equals(menuItemName)) {
						JKSecurityManager.getAuthorizer().checkAllowed(item.getPrivilige());
						return item;
					}
				}
			}
		}
		return null;
	}

	/**
	 * Gets the application frame.
	 *
	 * @return the applicationFrame
	 */
	public UIApplicationFrame getApplicationFrame() {
		return this.applicationFrame;
	}

	/**
	 * Gets the application id.
	 *
	 * @return the application id
	 */
	public int getApplicationId() {
		return this.applicationId;
	}

	/**
	 * Gets the application listeners.
	 *
	 * @return the application listeners
	 */
	public List getApplicationListeners() {
		return this.listeners;
	}

	/**
	 * Gets the application name.
	 *
	 * @return the applicationName
	 */
	public String getApplicationName() {
		return this.applicationName;
	}

	/**
	 * Gets the auto logout interval.
	 *
	 * @return the auto logout interval
	 */
	public int getAutoLogoutInterval() {
		return this.autoLogoutInterval;
	}

	/**
	 * Gets the config file name.
	 *
	 * @return the configFileName
	 */
	public String getConfigFileName() {
		return this.configFileName;
	}

	/**
	 * Gets the default module.
	 *
	 * @return the default module
	 */
	public Module getDefaultModule() {
		for (int i = 0; i < this.modules.size(); i++) {
			final Module module = this.modules.get(i);
			if (module.isDefault()) {
				return module;
			}
		}
		return null;
	}

	/**
	 * Gets the home image.
	 *
	 * @return the homeImage
	 */
	public String getHomeImage() {
		return this.homeImage;
	}

	/**
	 * Gets the in active locale.
	 *
	 * @return the in active locale
	 */
	public String getInActiveLocale() {
		return getLocale().equals("en") ? "ar" : "en";
	}

	/**
	 * Gets the locale.
	 *
	 * @return the locale
	 */
	public JKLocale getLocale() {
		return this.locale == null ? JK.DEFAULT_LOCALE : this.locale;
	}

	/**
	 * Gets the module.
	 *
	 * @param moduleName
	 *            the module name
	 * @return the module
	 */
	public Module getModule(final String moduleName) {
		for (final Module module : getModules()) {
			if (module.getModuleName().trim().equalsIgnoreCase(moduleName.trim())) {
				return module;
			}
		}
		return null;
	}

	/**
	 * Gets the modules.
	 *
	 * @return the modules
	 */
	public List getModules() {
		return this.modules;
	}

	/**
	 * Gets the splash image.
	 *
	 * @return the splashImage
	 */
	public String getSplashImage() {
		return this.splashImage;
	}

	/**
	 * Inits the.
	 *
	 * @throws ApplicationException
	 *             the application exception
	 */
	public void init() {
	}

	/**
	 * Checks if is allowed command.
	 *
	 * @param privilige
	 *            the privilige
	 * @param name
	 *            the name
	 * @return true, if is allowed command
	 */
	// ///////////////////////////////////////////////////////
	public boolean isAllowedCommand(final JKPrivilige privilige, final String name) {
		try {
			JKSecurityManager.getAuthorizer().checkAllowed(privilige);
			return true;
		} catch (final JKNotAllowedOperationException e) {
			System.err.println("Privlige Id : " + privilige.getPriviligeId() + " , with name : " + name + " is not allowed");
			return false;
		} catch (final SecurityException e) {
			JKExceptionUtil.handle(e);
			return false;
		}
	}

	/**
	 * Checks if is module defined.
	 *
	 * @param moduleName
	 *            the module name
	 * @return true, if is module defined
	 */
	public boolean isModuleDefined(final String moduleName) {
		for (final Module module : this.modules) {
			if (module.getModuleName().equals(moduleName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Checks if is view modules.
	 *
	 * @return the viewModules
	 */
	public boolean isViewModules() {
		return this.viewModules;
	}

	/**
	 * Sets the application frame.
	 *
	 * @param applicationFrame
	 *            the new application frame
	 */
	public void setApplicationFrame(final UIApplicationFrame applicationFrame) {
		this.applicationFrame = applicationFrame;
	}

	/**
	 * Sets the application id.
	 *
	 * @param applicationId
	 *            the new application id
	 */
	public void setApplicationId(final int applicationId) {
		this.applicationId = applicationId;
	}

	/**
	 * Sets the application name.
	 *
	 * @param applicationName
	 *            the applicationName to set
	 */
	public void setApplicationName(final String applicationName) {
		this.applicationName = applicationName;
		UserPreferences.setKeyPrefix(applicationName);
	}

	/**
	 * Sets the auto logout interval.
	 *
	 * @param autoLogoutInterval
	 *            the new auto logout interval
	 */
	public void setAutoLogoutInterval(final int autoLogoutInterval) {
		this.autoLogoutInterval = autoLogoutInterval;
	}

	/**
	 * Sets the config file name.
	 *
	 * @param configFileName
	 *            the new config file name
	 */
	public void setConfigFileName(final String configFileName) {
		this.configFileName = configFileName;
	}

	/**
	 * Sets the home image.
	 *
	 * @param homeImage
	 *            the homeImage to set
	 */
	public void setHomeImage(final String homeImage) {
		this.homeImage = homeImage;
	}

	/**
	 * Sets the locale.
	 *
	 * @param locale
	 *            the locale
	 * @param checkForUserPref
	 *            the check for user pref
	 */
	public void setLocale(final String locale, final boolean checkForUserPref) {
		final String localeKey = "locale";
		// to to find the default locale according to user pref , if not found ,
		// passed defaut will be used and
		// and stored as user pref
		if (checkForUserPref) {
			this.locale = new JKLocale(UserPreferences.get(localeKey, locale));
		} else {
			this.locale = new JKLocale(locale);
		}
		UserPreferences.put(localeKey, this.locale.getLanguageShortName());
	}

	/**
	 * Sets the modules.
	 *
	 * @param modules
	 *            the modules to set
	 */
	public void setModules(final List modules) {
		this.modules = modules;
	}

	/**
	 * Sets the splash image.
	 *
	 * @param splashImage
	 *            the splashImage to set
	 */
	public void setSplashImage(final String splashImage) {
		this.splashImage = splashImage;
	}

	/**
	 * Sets the view modules.
	 *
	 * @param viewModules
	 *            the new view modules
	 */
	public void setViewModules(final boolean viewModules) {
		this.viewModules = viewModules;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuffer buf = new StringBuffer("Name : " + getApplicationName() + "\n");
		for (int i = 0; i < this.modules.size(); i++) {
			buf.append(this.modules.get(i).toString());
		}
		return buf.toString();
	}

	/**
	 * Gets the background image.
	 *
	 * @return the background image
	 */
	public String getBackgroundImage() {
		return backgroundImage == null ? getHomeImage() : backgroundImage;
	}

	/**
	 * Sets the background image.
	 *
	 * @param backgroundImage
	 *            the new background image
	 */
	public void setBackgroundImage(String backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy