BrightSide Workbench Full Report + Source Code
TimeTrackerCtrl.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 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.timetracker;
20 
21 import java.util.Map;
22 import org.amic.util.date.CheckDate;
23 import org.turro.string.Strings;
24 import org.turro.action.Plugins;
25 import org.turro.annotation.ElephantPlugin;
26 import org.turro.auth.Authentication;
27 import org.turro.collections.KeyValueMap;
28 import org.turro.elephant.context.IConstructor;
29 import org.turro.elephant.direct.AbstractDirectContentCtrl;
30 import org.turro.elephant.direct.DirectContent;
31 import org.turro.elephant.entities.db.TimeTrackerType;
32 import org.turro.entities.Entities;
33 import org.turro.marker.ElephantMarker;
34 import org.turro.plugin.contacts.IContact;
35 
40 @ElephantPlugin(label="timetracker-ctrl")
41 @DirectContent(identifier="timetracker-action")
43 
44  private final static String TTCONTEXT = "time-tracker-entity-path";
45 
46  public TimeTrackerCtrl() {
47  super("timetracker");
48  setTemplate("track");
49  }
50 
51  @Override
52  protected void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm) {
53  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
54  }
55 
56  @Override
57  protected void prepareMarker(ElephantMarker marker) {
58  String entityPath = (String) marker.getConstructor().getSessionAttribute(TTCONTEXT);
59  marker.put("entityPath", entityPath);
60  marker.put("iee", Entities.getController(entityPath));
61  marker.put("queue", getQueue(entityPath, false));
62  marker.put("valids", getQueue(entityPath, false).getNextValids());
63  CheckDate dateTrack = new CheckDate();
64  marker.put("year", dateTrack.getYear());
65  marker.put("month", dateTrack.getMonth());
66  marker.put("day", dateTrack.getDay());
67  marker.put("hour", dateTrack.getHour());
68  marker.put("minute", dateTrack.getMinute());
69  }
70 
71  /* IDirectContent */
72 
73  @Override
74  protected String getIdentifier() {
75  return TimeTrackerCtrl.class.getAnnotation(DirectContent.class).identifier();
76  }
77 
78  @Override
79  protected void doExecute(IConstructor constructor, KeyValueMap map) {
80  String entityPath = (String) constructor.getSessionAttribute(TTCONTEXT);
81  String type = map.get("type");
82  if("track".equals(type)) {
83  if(map.containsKey("trackdelete")) {
84  getQueue((String) constructor.getSessionAttribute(TTCONTEXT), true).deleteLastTrack();
85  } else if(map.containsKey("tracktype")) {
86  CheckDate dateTrack = new CheckDate(
87  map.get(Integer.class, "year"),
88  map.get(Integer.class, "month"),
89  map.get(Integer.class, "day"),
90  map.get(Integer.class, "hour"),
91  map.get(Integer.class, "minute"),
92  -1);
93  if(getQueue(entityPath, true).correctDate(dateTrack)) {
94  getQueue(entityPath, false).addTrack(TimeTrackerType.valueOf(map.get("tracktype")), dateTrack.getDate());
95  constructor.removeSessionAttribute(TTCONTEXT);
96  }
97  } else {
98  constructor.removeSessionAttribute(TTCONTEXT);
99  }
100  } else if("entity".equals(type)) {
101  constructor.setSessionAttribute(TTCONTEXT, map.get("entityPath"));
102  }
103  }
104 
105  /* TimeTrackerCtrl */
106 
107  @Override
108  public void render(IConstructor constructor) {
109  String entityPath = (String) constructor.getSessionAttribute(TTCONTEXT);
110  if(!Strings.isBlank(entityPath) && (getQueue(entityPath, true) != null)) {
111  super.render(constructor);
112  } else {
113  ElephantMarker marker = new ElephantMarker(constructor);
114  marker.put("actions", this);
115  IContact contact = Authentication.getIContact();
116  if(contact.getSyndications().contains("employee") ||
117  contact.getSyndications().contains("manager") ||
118  contact.getSyndications().contains("financials_admin")) {
119  Map<String, Object> args = Plugins.execute("CurrentCompany", null);
120  marker.put("stores", args.get("departmentStores"));
121  }
122  if(!Strings.isBlank(entityPath)) {
123  marker.put("openTracks", getQueue(entityPath, false).getActiveTracks());
124  }
125  marker.process("timetracker", "selectEntity");
126  }
127  }
128 
129  public String getActionLink(IConstructor constructor, TimeTrackerType type) {
130  return createRightNowURL("type=track;tracktype=" + type.toString());
131  }
132 
133  /* Queue */
134 
135  private TimeTrackerQueue ttq;
136 
137  private TimeTrackerQueue getQueue(String entityPath, boolean reset) {
138  if(reset || (ttq == null && !Strings.isBlank(entityPath))) {
139  IContact contact = Authentication.getIContact();
140  ttq = TimeTrackerQueue.load(entityPath, "/contact/" + contact.getId());
141  }
142  return ttq;
143  }
144 
145 }
static Map< String, Object > execute(String name, Map params)
Definition: Plugins.java:44
static IElephantEntity getController(String path)
Definition: Entities.java:78
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)
void doExecute(IConstructor constructor, KeyValueMap map)
String getActionLink(IConstructor constructor, TimeTrackerType type)
void render(IConstructor constructor)
void prepareCleanMarker(ElephantMarker em, KeyValueMap kvm)
void prepareMarker(ElephantMarker marker)
static TimeTrackerQueue load(String entityPath, String trackedPath)
void setSessionAttribute(String key, Object value)