--- /dev/null
+/*****************************************************************************
+ * Copyright Igor Barma, Eric Brun, Alexandre Desoubeaux, Christian Martel,
+ * (2 décembre 2008)
+ *
+ * Ce logiciel est un programme informatique servant à l'évaluation des
+ * compétences.
+ *
+ * Ce logiciel est régi par la licence CeCILL soumise au droit français et
+ * respectant les principes de diffusion des logiciels libres. Vous pouvez
+ * utiliser, modifier et/ou redistribuer ce programme sous les conditions
+ * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
+ * sur le site "http://www.cecill.info".
+ *
+ * En contrepartie de l'accessibilité au code source et des droits de copie,
+ * de modification et de redistribution accordés par cette licence, il n'est
+ * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
+ * seule une responsabilité restreinte pèse sur l'auteur du programme, le
+ * titulaire des droits patrimoniaux et les concédants successifs.
+ *
+ * A cet égard l'attention de l'utilisateur est attirée sur les risques
+ * associés au chargement, à l'utilisation, à la modification et/ou au
+ * développement et à la reproduction du logiciel par l'utilisateur étant
+ * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
+ * manipuler et qui le réserve donc à des développeurs et des professionnels
+ * avertis possédant des connaissances informatiques approfondies. Les
+ * utilisateurs sont donc invités à charger et tester l'adéquation du
+ * logiciel à leurs besoins dans des conditions permettant d'assurer la
+ * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
+ * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
+ *
+ * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
+ * pris connaissance de la licence CeCILL, et que vous en avez accepté les
+ * termes.
+ *******************************************************************************/
+package com.pentila.evalcomp.client.utilities;
+
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import ua.metallic.ofcchart.client.ChartWidget;
+import ua.metallic.ofcchart.client.model.ChartData;
+import ua.metallic.ofcchart.client.model.Legend;
+import ua.metallic.ofcchart.client.model.Legend.Position;
+import ua.metallic.ofcchart.client.model.Text;
+import ua.metallic.ofcchart.client.model.elements.PieChart;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.IFrameElement;
+import com.google.gwt.http.client.URL;
+import com.google.gwt.i18n.client.DateTimeFormat;
+import com.google.gwt.regexp.shared.MatchResult;
+import com.google.gwt.regexp.shared.RegExp;
+import com.google.gwt.user.client.ui.Frame;
+import com.gwtext.client.core.Ext;
+import com.gwtext.client.core.ExtElement;
+import com.gwtext.client.core.Function;
+import com.gwtext.client.data.Record;
+import com.gwtext.client.data.Store;
+import com.gwtext.client.widgets.Tool;
+import com.gwtext.client.widgets.layout.FitLayout;
+import com.pentila.evalcomp.client.i18n.Messages;
+import com.pentila.evalcomp.client.widget.ExtWindow;
+import com.pentila.evalcomp.domain.definition.Property;
+
+
+// TODO: Auto-generated Javadoc
+/**
+ * The Class Tools. Some tools and constants used inside the application
+ */
+public class Tools {
+
+ /** The Constant EVAL_INST_INTERFACE. */
+ public static final int EVAL_INST_INTERFACE = 2;
+ /** The Constant EVAL_PEDA_INTERFACE. */
+ public static final int EVAL_PEDA_INTERFACE = 1;
+
+ /** The Constant EVAL_TECH_INTERFACE. */
+ public static final int EVAL_TECH_INTERFACE = 3;
+
+ /** The interface shown. */
+ public static int interfaceShown = 0;
+
+ /** The I s_ inst. */
+ public static boolean IS_INST = false;
+
+ /** The I s_ peda. */
+ public static boolean IS_PEDA = false;
+
+ /** The I s_ tech. */
+ public static boolean IS_TECH = false;
+
+ /** The loading widget. */
+ public static LoadingWidget loadingWidget = null;
+
+ /** The Constant NUM_MILLISECS_A_DAY. */
+ public static final long NUM_MILLISECS_A_DAY = (24 * 60 * 60 * 1000);
+
+ /** The Constant ROLE_GEST_INST. */
+ public static final String ROLE_GEST_INST = "ROLE_MANAGER";
+
+ /** The Constant ROLE_GEST_PEDA. */
+ public static final String ROLE_GEST_PEDA = "ROLE_PEDA";
+
+ /** The Constant ROLE_GEST_TECH. */
+ public static final String ROLE_GEST_TECH = "ROLE_TECH";
+
+ /** The Constant ROLE_GEST_SCO. */
+ public static final String ROLE_GEST_SCO = "ROLE_SCO";
+
+ /** The Constant ROLE_PARTICIPANT. */
+ public static final String ROLE_PARTICIPANT = "";
+
+ /** The Constant USER_INTERFACE. */
+ public static final int USER_INTERFACE = 0;
+
+ /**
+ * Adds the days.
+ *
+ * @param d
+ * the d
+ * @param nbDays
+ * the nb days
+ *
+ * @return the date
+ */
+ public static Date addDays(Date d, int nbDays) {
+ long currentTime = d.getTime();
+ long m_days = NUM_MILLISECS_A_DAY * nbDays;
+ long newTime = currentTime + m_days;
+ Date dd = new Date(newTime);
+ return dd;
+ }
+
+ /**
+ * Delta days.
+ *
+ * @param db
+ * the db
+ * @param de
+ * the de
+ * @param defaultDelta
+ * the default delta
+ *
+ * @return the int
+ */
+ public static int deltaDays(Date db, Date de, int defaultDelta) {
+ if (db == null || de == null) {
+ return defaultDelta;
+ }
+ long deltaTime = de.getTime() - db.getTime();
+ long days = (deltaTime / NUM_MILLISECS_A_DAY);
+ return (new Long(days)).intValue();
+ }
+
+ /**
+ * Escape quote.
+ *
+ * @param text
+ * the text
+ *
+ * @return the string
+ */
+ public static String escapeQuote(String text) {
+ text = text.replaceAll("'", "’");
+ text = text.replaceAll("\"", """);
+ text = text.replaceAll("\n|\r", "<br/>");
+ text = text.replaceAll("\t", " ");
+ return text;
+
+ }
+
+
+ /**
+ * Gets the jSPURL.
+ *
+ * @return the jSPURL
+ */
+ public static final String getJSPURL(){
+ String tmpURL = "";
+
+ tmpURL = GWT.getHostPageBaseURL();
+
+
+
+ return tmpURL;
+ }
+
+ /**
+ * Gets the base url.
+ *
+ * @return the base url
+ */
+ public static final String getBaseURL() {
+ String url = "";
+ if (GWT.isScript()) {
+ url = GWT.getModuleBaseURL();
+ } else {
+ url = GWT.getHostPageBaseURL();
+ }
+ return url;
+ }
+
+ /**
+ * Gets the date formated.
+ *
+ * @param date
+ * the date
+ *
+ * @return the date formated
+ */
+ public static String getDateFormated(Date date) {
+ if (date == null) {
+ return Messages.getString("date_non_definie");
+ }
+
+ DateTimeFormat dtf = DateTimeFormat.getFormat(Messages
+ .getString("format_date_utils"));
+ return dtf.format(date);
+ }
+
+ /**
+ * Gets the date formated.
+ *
+ * @param date the date
+ * @param format the format
+ * @return the date formated
+ */
+ public static String getDateFormated(Date date, String format) {
+ if (date == null) {
+ return Messages.getString("date_non_definie");
+ }
+ if (format == null){
+ format = "format_date_utils";
+ }
+
+ DateTimeFormat dtf = DateTimeFormat.getFormat(Messages
+ .getString(format));
+ return dtf.format(date);
+ }
+
+ public static String getSimpleDateFormated(Date date, String format){
+ if (date == null) {
+ return "";
+ }
+ DateTimeFormat dtf = DateTimeFormat.getFormat(format);
+ return dtf.format(date);
+ }
+
+ public static String getMinimalDateFormated(Date date, boolean hour){
+ String result = "";
+ if (date != null) {
+ DateTimeFormat dateFormat1 = DateTimeFormat.getFormat("EE dd MMMM");
+ result += dateFormat1.format(date);
+
+ if(date.getYear() != (new Date()).getYear()){
+ DateTimeFormat dateFormat2 = DateTimeFormat.getFormat("yyyy");
+ result += " " + dateFormat2.format(date);
+ }
+ if (hour){
+ result += " " + DateTimeFormat.getFormat("HH:mm:ss").format(date);
+ }
+ }
+ return result;
+ }
+
+
+ /**
+ * Gets the date for sort.
+ *
+ * @param date
+ * the date
+ *
+ * @return the date for sort
+ */
+ public static String getDateForSort(Date date) {
+ DateTimeFormat dtf = DateTimeFormat.getFormat("yyyyMMddHHmmss");
+ return dtf.format(date);
+ }
+
+ /**
+ * Gets the hour formated.
+ *
+ * @param date
+ * the date
+ *
+ * @return the hour formated
+ */
+ public static String getHourFormated(Date date) {
+ if (date == null) {
+ return Messages.getString("heure_non_definie");
+ }
+ DateTimeFormat dtf = DateTimeFormat.getFormat(Messages
+ .getString("forma_heure_utils"));
+ return dtf.format(date);
+ }
+
+ /**
+ * Gets the date formated to iso8601.
+ *
+ * @param date
+ * the date
+ *
+ * @return the date formated
+ */
+ public static String getISO8601DateFormated(Date date) {
+ if (date == null) {
+ return "2008-11-01"; //$NON-NLS-1$
+ }
+ DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
+ return dtf.format(date);
+ }
+
+ /**
+ * Gets the logout url.
+ *
+ * @return the logout url
+ */
+ public static final String getLogoutURL() {
+ String tmpURL = "";
+
+ tmpURL = GWT.getHostPageBaseURL();
+
+
+ tmpURL = tmpURL + "logout.jsp";
+ return tmpURL;
+ }
+
+ /**
+ * Gets the url of the ressource jsp.
+ *
+ * @return the ressource access url
+ */
+ public static final String getRessourceAccessURL() {
+ String tmpURL = "";
+
+ tmpURL = GWT.getHostPageBaseURL();
+
+ tmpURL = tmpURL + "Ressource.jsp";
+
+ return tmpURL;
+ }
+
+ /**
+ * Gets the url of the ressource jsp.
+ *
+ * @return the ressource access url
+ */
+ public static final String getExportAccessURL() {
+ String tmpURL = "";
+
+ tmpURL = GWT.getHostPageBaseURL();
+
+
+ tmpURL = tmpURL + "exportvalidation.jsp";
+
+
+ return tmpURL;
+ }
+
+ /**
+ * Gets the url of the exportInstanceTable jsp.
+ *
+ * @return the ressource access url
+ */
+ public static final String getexportInstanceTableURL() {
+ String tmpURL = "";
+
+ tmpURL = GWT.getHostPageBaseURL();
+
+
+ tmpURL = tmpURL + "exportInstanceTable.jsp";
+
+
+ return tmpURL;
+ }
+
+ /**
+ * Hide loading.
+ */
+ public static void hideLoading() {
+ if (loadingWidget != null){
+ loadingWidget.hideLoading();
+ }
+ }
+
+ /**
+ * Hide loading region.
+ *
+ * @param regionId
+ * the region id
+ */
+ public static void hideLoadingRegion(String regionId) {
+ ExtElement element = Ext.get(regionId);
+ if (element != null && !Ext.isIE()) {
+ element.unmask();
+
+ } else {
+ hideLoading();
+ }
+ hideLoading();
+ }
+
+ // ouvre dans le browser l'url donnée dans une nouvelle fenetre
+ /**
+ * Open window.
+ *
+ * @param url
+ * the url
+ * @param name
+ * the name
+ */
+ public static void openWindow(String url, String name) {
+ com.google.gwt.user.client.Window.open(url, "_blank", "");
+ }
+
+ /**
+ * Display ressource url.
+ *
+ * @param url the url
+ * @param path the path
+ * @param name the name
+ * @param iconCls the icon cls
+ */
+ public static void displayRessourceUrl(String url, String path, final String name, String iconCls) {
+
+ final String urlreel = url;
+
+ final ExtWindow w = new ExtWindow();
+
+
+
+ w.addTool(new Tool(Tool.PLUS, new Function(){
+
+ public void execute() {
+
+ openWindow(urlreel, name);
+
+ }
+
+ }, Messages.getString("Ouvrir_dans_un_nouvel_onglet")));
+
+ w.setMaximizable(true);
+ w.setCollapsible(true);
+
+ w.setTitle(name, iconCls);
+ w.setLayout(new FitLayout());
+
+
+
+ Frame r = new Frame(urlreel);
+
+
+ // IE set frame border to 0
+ final IFrameElement e = IFrameElement.as(r.getElement());
+ e.setFrameBorder(0);
+
+ w.add(r);
+ w.setHtml(urlreel);
+ w.setSize(800, 600);
+
+ w.show();
+ w.getEl().setStyle("z-index", "1000000");
+ /*final DelayedTask dt = new DelayedTask();
+ final Function doit = new Function(){
+ public void execute() {
+ try{
+ System.out.println(e.getContentDocument().getBody().getInnerHTML());
+
+ w.close();
+
+ }catch(Exception e){
+ System.out.println("Echo errror");
+ }
+
+ }
+
+ };
+
+
+ dt.delay(1500, doit);
+ */
+
+
+ }
+
+ public static String getTwoDigit(long v){
+
+ String value = "" + v;
+ if (value.length() == 1){
+ value = "0" + value;
+ }
+ return value;
+ }
+
+ // redirige le browser sur l'url donnée
+ /**
+ * Redirect.
+ *
+ * @param url
+ * the url
+ */
+ public static native void redirect(String url)/*-{
+ $wnd.location = url;
+ }-*/;
+
+ /**
+ * Show loading.
+ *
+ * @param text
+ * the text
+ * @param progressText
+ * the progress text
+ */
+ public static void showLoading(final String text, final String progressText) {
+ if (loadingWidget == null) {
+ loadingWidget = new LoadingWidget();
+ }
+ loadingWidget.showLoading(text, progressText);
+ }
+
+ /**
+ * Show loading region.
+ *
+ * @param regionId
+ * the region id
+ * @param text
+ * the text
+ * @param progressText
+ * the progress text
+ */
+ public static void showLoadingRegion(String regionId, final String text,
+ final String progressText) {
+ ExtElement element = Ext.get(regionId);
+ if (element != null && !Ext.isIE()) {
+ element.mask(text);
+ } else {
+ showLoading(text, progressText);
+ }
+ }
+
+ /**
+ * Store finder.
+ *
+ * @param store
+ * the store
+ * @param field
+ * the field
+ * @param value
+ * the value
+ *
+ * @return the record
+ */
+ public static Record storeFinder(Store store, String field, String value) {
+ for (Record rec : store.getRecords()) {
+ try {
+ if (rec.getAsString(field).equals(value)) {
+ return rec;
+ }
+ } catch (Exception ex) {
+ }
+ }
+ return null;
+ }
+
+
+
+ /**
+ * Format html label.
+ *
+ * @param text the text
+ * @param iconClass the icon class
+ * @return the string
+ */
+ public static String formatHtmlLabel(String text, String iconClass){
+ return "<table><tr><td width='16px' height='16px' class='" + iconClass + "'></td><td>" + text + "</td></tr><table>";
+ }
+
+
+
+ /**
+ * Gets the parent path.
+ *
+ * @param path the path
+ * @return the parent path
+ */
+ public static String getParentPath(String path){
+ return path.substring(0, path.lastIndexOf("/"));
+ }
+
+
+ /**
+ * Gets the leaf path.
+ *
+ * @param path the path
+ * @return the leaf path
+ */
+ public static String getLeafPath(String path){
+ return path.substring(path.lastIndexOf("/")+1);
+ }
+
+ /**
+ * Wrap text.
+ *
+ * @param myText the my text
+ * @param maxLength the max length
+ * @return the string
+ */
+ public static String wrapText(String myText, int maxLength){
+ if (myText == null){
+ return "";
+ }
+
+ String [] split = myText.split(" ");
+ int count = 0;
+ String f = "";
+
+ for (String word : split){
+
+ count = count + word.length()+1;
+ if (count > maxLength){
+ // insert <br> before add this word
+ f = f + "<br/>";
+ count = 0;
+ }else{
+ f = f + " ";
+ }
+
+ f = f + word;
+
+ }
+ f = f.trim();
+ return f;
+ }
+
+
+ public static String encodePath(String path){
+
+ String[] array = path.split("/");
+
+ String ret = "";
+ int i = 0;
+ while( i < array.length ){
+
+ String token = URL.encodeQueryString(array[i++]);
+ ret += token;
+ if (i != array.length){
+ ret += "/";
+ }
+
+ }
+
+ return ret;
+
+ }
+
+ public static ChartWidget getChart(Map<String, Integer> map, String title, boolean legend, String width, String height){
+ ChartWidget chart = new ChartWidget();
+
+
+ String swfurl = Tools.getBaseURL() + "ofcgwt/open-flash-chart.swf";
+
+ chart.setSrc(swfurl);
+ chart.setMargin(0);
+
+
+ ChartData cd = new ChartData();
+
+ cd.setTitle(new Text(title));
+
+ if (legend){
+ Legend l = new Legend(Position.RIGHT, false);
+ l.setBorder(true);
+ l.setShadow(true);
+ cd.setLegend(l);
+ }
+ cd.setBackgroundColour("#ffffff");
+
+ PieChart pie = new PieChart();
+
+ pie.setAlpha(0.5f);
+ pie.setNoLabels(true);
+ pie.setTooltip("#label#: #val#<br>#percent#");
+ pie.setAnimateOnShow(true);
+ pie.setGradientFill(true);
+
+
+ List<String> colors = new ArrayList<String>();
+
+ pie.setBorder(0);
+
+ for (Entry<String, Integer> e: map.entrySet()){
+
+ String key = e.getKey();
+
+
+ if (key.equals("total")){
+ continue;
+ }
+
+ Integer value = e.getValue();
+
+ pie.addSlices(new PieChart.Slice(value, Messages
+ .getString("OFC_"+key)));
+
+ colors.add(Messages.getString("OFC_" + key + "_color"));
+ }
+
+ pie.setColours(colors);
+
+
+ cd.addElements(pie);
+ chart.setSize(width, height);
+
+ chart.setJsonData(cd.buildJSON().toString());
+ return chart;
+ }
+
+ public static String getSubTypeProperty(Property p){
+
+ String subType = null;
+
+ String description = p.getDescription();
+
+ RegExp re = RegExp.compile("/\\*(.*)\\*/");
+
+
+ MatchResult mr = re.exec(description);
+
+
+
+
+ if (mr != null) {
+
+
+
+ String ext = mr.getGroup(1);
+
+ if (ext.startsWith("type=")){
+ subType = ext.split("type=")[1];
+ }
+
+
+
+ }
+
+ return subType;
+ }
+
+
+ public static native String base64decode(final String data) /*-{
+
+ var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+ var out = "", c1, c2, c3, e1, e2, e3, e4;
+ for (var i = 0; i < data.length; ) {
+ e1 = tab.indexOf(data.charAt(i++));
+ e2 = tab.indexOf(data.charAt(i++));
+ e3 = tab.indexOf(data.charAt(i++));
+ e4 = tab.indexOf(data.charAt(i++));
+ c1 = (e1 << 2) + (e2 >> 4);
+ c2 = ((e2 & 15) << 4) + (e3 >> 2);
+ c3 = ((e3 & 3) << 6) + e4;
+ out += String.fromCharCode(c1);
+ if (e3 != 64)
+ out += String.fromCharCode(c2);
+ if (e4 != 64)
+ out += String.fromCharCode(c3);
+ }
+ return out;
+
+ }-*/;
+
+ public static native String base64encode(final String data) /*-{
+var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
+ var out = "", c1, c2, c3, e1, e2, e3, e4;
+for (var i = 0; i < data.length; ) {
+c1 = data.charCodeAt(i++);
+c2 = data.charCodeAt(i++);
+c3 = data.charCodeAt(i++);
+e1 = c1 >> 2;
+e2 = ((c1 & 3) << 4) + (c2 >> 4);
+e3 = ((c2 & 15) << 2) + (c3 >> 6);
+e4 = c3 & 63;
+if (isNaN(c2))
+e3 = e4 = 64;
+else if (isNaN(c3))
+e4 = 64;
+out += tab.charAt(e1) + tab.charAt(e2) + tab.charAt(e3) + tab.charAt(e4);
+}
+return out;
+
+ }-*/;
+
+
+}