BrightSide Workbench Full Report + Source Code
TemplateControl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2016 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.elephant;
20 
21 import java.io.File;
22 import java.io.FileReader;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.auth.Authentication;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.context.IConstructor;
32 import org.turro.marker.ElephantMarker;
33 import org.turro.security.FlatPermissions;
34 import static org.zkoss.zk.ui.Component.SESSION_SCOPE;
35 import org.zkoss.zk.ui.Executions;
36 import org.zkoss.zk.ui.IdSpace;
37 import org.zkoss.zk.ui.ext.AfterCompose;
38 import org.zkoss.zk.ui.select.Selectors;
39 import org.zkoss.zul.Div;
40 
45 public class TemplateControl extends Div implements IdSpace, AfterCompose {
46 
47  private String rootTmpl, tmpl;
48 
49  public void setRootTmpl(String rootTmpl) {
50  this.rootTmpl = rootTmpl;
51  }
52 
53  public void setTmpl(String tmpl) {
54  this.tmpl = tmpl;
55  }
56 
57  @Override
58  public void afterCompose() {
60  getPage().setAttribute("rolemap", Authentication.getIContact().getPermissions().asMap(), SESSION_SCOPE);
61  } else {
62  getPage().setAttribute("rolemap", new FlatPermissions().asMap(), SESSION_SCOPE);
63  }
64  initControl();
65  doFinally();
66  }
67 
68  protected void doFinally() {
69  }
70 
71  private void initControl() {
72  File file = null;
74  if(ElephantMarker.existsTemplate(constructor, false, rootTmpl, tmpl, true)) {
75  file = ElephantMarker.fileTemplate(constructor, false, rootTmpl, tmpl, true);
76  } else if(ElephantMarker.existsTemplate(constructor, false, rootTmpl, tmpl)) {
77  file = ElephantMarker.fileTemplate(constructor, false, rootTmpl, tmpl);
78  }
79  if(file != null) {
80  try {
81  Map<String, Object> args = new HashMap<>();
82  args.put("tmplctrl", this);
83  Executions.createComponentsDirectly(new FileReader(file), null, this, args);
84  Selectors.wireComponents(this, this, false);
85  Selectors.wireEventListeners(this, this);
86  } catch (IOException ex) {
87  Logger.getLogger(TemplateControl.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
88  }
89  }
90  }
91 
92 }
static File fileTemplate(IConstructor constructor, boolean mail, String root, String name)
static boolean existsTemplate(IConstructor constructor, boolean mail, String root, String name)