| 1 | package com.itmill.incubator.examples.cms; |
|---|
| 2 | |
|---|
| 3 | import java.math.BigInteger; |
|---|
| 4 | import java.text.DateFormat; |
|---|
| 5 | import java.text.SimpleDateFormat; |
|---|
| 6 | import java.util.Date; |
|---|
| 7 | import java.util.Map; |
|---|
| 8 | import java.util.Set; |
|---|
| 9 | |
|---|
| 10 | import org.hibernate.SQLQuery; |
|---|
| 11 | import org.hibernate.Session; |
|---|
| 12 | |
|---|
| 13 | import com.itmill.incubator.examples.cms.data.Degree; |
|---|
| 14 | import com.itmill.incubator.examples.cms.data.HibernateUtil; |
|---|
| 15 | import com.itmill.incubator.examples.cms.data.Job; |
|---|
| 16 | import com.itmill.incubator.examples.cms.data.Person; |
|---|
| 17 | import com.itmill.incubator.examples.cms.data.Project; |
|---|
| 18 | import com.itmill.incubator.examples.cms.data.Publication; |
|---|
| 19 | import com.itmill.incubator.examples.cms.data.Skill; |
|---|
| 20 | import com.itmill.toolkit.data.hbnutil.HbnContainer.SessionManager; |
|---|
| 21 | import com.itmill.toolkit.event.Action; |
|---|
| 22 | import com.itmill.toolkit.event.Action.Handler; |
|---|
| 23 | import com.itmill.toolkit.ui.Button; |
|---|
| 24 | import com.itmill.toolkit.ui.DateField; |
|---|
| 25 | import com.itmill.toolkit.ui.ExpandLayout; |
|---|
| 26 | import com.itmill.toolkit.ui.FormLayout; |
|---|
| 27 | import com.itmill.toolkit.ui.Layout; |
|---|
| 28 | import com.itmill.toolkit.ui.OrderedLayout; |
|---|
| 29 | import com.itmill.toolkit.ui.Panel; |
|---|
| 30 | import com.itmill.toolkit.ui.Select; |
|---|
| 31 | import com.itmill.toolkit.ui.Table; |
|---|
| 32 | import com.itmill.toolkit.ui.TextField; |
|---|
| 33 | import com.itmill.toolkit.ui.Window; |
|---|
| 34 | import com.itmill.toolkit.ui.Button.ClickEvent; |
|---|
| 35 | import com.itmill.toolkit.ui.Button.ClickListener; |
|---|
| 36 | |
|---|
| 37 | public class EditorPerson extends Window implements ClickListener { |
|---|
| 38 | private TextField name = new TextField("Name"); |
|---|
| 39 | private TextField cellphone = new TextField("Cellphone"); |
|---|
| 40 | private TextField email = new TextField("Email"); |
|---|
| 41 | private TextField nationality = new TextField("Nationality"); |
|---|
| 42 | private DateField birthdate = new DateField("Birthdate"); |
|---|
| 43 | private Table publications = new Table("Publications"); |
|---|
| 44 | private Table jobs = new Table("Jobs"); |
|---|
| 45 | private Table skills = new Table("Skills"); |
|---|
| 46 | private Table degrees = new Table("Degrees"); |
|---|
| 47 | private Table projects = new Table("Projects"); |
|---|
| 48 | |
|---|
| 49 | private Button preSave = new Button("Save"); |
|---|
| 50 | private Button cancel = new Button("Cancel"); |
|---|
| 51 | private Button save = new Button("Save"); |
|---|
| 52 | private Button delete = new Button("Delete"); |
|---|
| 53 | |
|---|
| 54 | private Button addPublications = new Button("Add Publications"); |
|---|
| 55 | private Button addSkill = new Button("Add Skill"); |
|---|
| 56 | private Button addDegree = new Button("Add Degree"); |
|---|
| 57 | private Button addJob = new Button("Add Job"); |
|---|
| 58 | private Button addProject = new Button("Add Project"); |
|---|
| 59 | |
|---|
| 60 | private SessionManager sess; |
|---|
| 61 | private ViewAdmin viewAdmin; |
|---|
| 62 | private Layout w = new ExpandLayout(); |
|---|
| 63 | private FormLayout form = new FormLayout(); |
|---|
| 64 | private Person pojo; |
|---|
| 65 | |
|---|
| 66 | public EditorPerson(Person defaults, String caption, ViewAdmin viewAdmin, |
|---|
| 67 | SessionManager sess) { |
|---|
| 68 | super(caption); |
|---|
| 69 | this.sess = sess; |
|---|
| 70 | this.viewAdmin = viewAdmin; |
|---|
| 71 | pojo = defaults; |
|---|
| 72 | birthdate.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 73 | birthdate.setWidth(100); |
|---|
| 74 | |
|---|
| 75 | setHeight(600); |
|---|
| 76 | setWidth(700); |
|---|
| 77 | setLayout(w); |
|---|
| 78 | w.setStyleName(Panel.STYLE_LIGHT); |
|---|
| 79 | |
|---|
| 80 | publications.setHeight(100); |
|---|
| 81 | publications.setWidth(600); |
|---|
| 82 | publications.addActionHandler(new Handler() { |
|---|
| 83 | Action add = new Action("add"); |
|---|
| 84 | Action delete = new Action("delete"); |
|---|
| 85 | Action[] actions = { add, delete }; |
|---|
| 86 | |
|---|
| 87 | public Action[] getActions(Object target, Object sender) { |
|---|
| 88 | return actions; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public void handleAction(Action action, Object sender, Object target) { |
|---|
| 92 | if (action == add) { |
|---|
| 93 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 94 | new EditPublication(EditorPerson.this.viewAdmin, |
|---|
| 95 | pojo)); |
|---|
| 96 | } else if (action == delete) { |
|---|
| 97 | Session s = HibernateUtil.getSessionFactory() |
|---|
| 98 | .getCurrentSession(); |
|---|
| 99 | s.beginTransaction(); |
|---|
| 100 | |
|---|
| 101 | s.createSQLQuery( |
|---|
| 102 | "DELETE FROM Person_Publication WHERE authors_id = " |
|---|
| 103 | + pojo.getId() + " AND publications_id = " |
|---|
| 104 | + target).executeUpdate(); |
|---|
| 105 | s.flush(); |
|---|
| 106 | |
|---|
| 107 | pojo = (Person) s.load(Person.class, pojo.getId()); |
|---|
| 108 | s.merge(pojo); |
|---|
| 109 | s.flush(); |
|---|
| 110 | |
|---|
| 111 | updatePublications(); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | }); |
|---|
| 115 | |
|---|
| 116 | jobs.setHeight(100); |
|---|
| 117 | jobs.setWidth(600); |
|---|
| 118 | jobs.addActionHandler(new Handler() { |
|---|
| 119 | Action add = new Action("add"); |
|---|
| 120 | Action edit = new Action("edit"); |
|---|
| 121 | Action delete = new Action("delete"); |
|---|
| 122 | Action[] actions = { add, edit, delete }; |
|---|
| 123 | |
|---|
| 124 | public Action[] getActions(Object target, Object sender) { |
|---|
| 125 | return actions; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | public void handleAction(Action action, Object sender, Object target) { |
|---|
| 129 | if (action == add) { |
|---|
| 130 | EditorPerson.this.viewAdmin.getWindow() |
|---|
| 131 | .addWindow( |
|---|
| 132 | new EditJob(EditorPerson.this.viewAdmin, |
|---|
| 133 | pojo, null)); |
|---|
| 134 | } else if (action == edit) { |
|---|
| 135 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 136 | new EditJob(EditorPerson.this.viewAdmin, pojo, |
|---|
| 137 | target)); |
|---|
| 138 | } else if (action == delete) { |
|---|
| 139 | Session s = HibernateUtil.getSessionFactory() |
|---|
| 140 | .getCurrentSession(); |
|---|
| 141 | s.beginTransaction(); |
|---|
| 142 | |
|---|
| 143 | s.createSQLQuery( |
|---|
| 144 | "DELETE FROM Job WHERE employee_id = " |
|---|
| 145 | + pojo.getId() + " AND id = " + target) |
|---|
| 146 | .executeUpdate(); |
|---|
| 147 | |
|---|
| 148 | // Hibernate passes a kidney stone when combining custom |
|---|
| 149 | // queries and O/R |
|---|
| 150 | pojo = (Person) s.load(Person.class, pojo.getId()); |
|---|
| 151 | s.merge(pojo); |
|---|
| 152 | s.flush(); |
|---|
| 153 | |
|---|
| 154 | updateJobs(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | }); |
|---|
| 158 | |
|---|
| 159 | skills.setHeight(100); |
|---|
| 160 | skills.setWidth(600); |
|---|
| 161 | skills.addActionHandler(new Handler() { |
|---|
| 162 | Action add = new Action("add"); |
|---|
| 163 | Action edit = new Action("edit"); |
|---|
| 164 | Action delete = new Action("delete"); |
|---|
| 165 | Action[] actions = { add, edit, delete }; |
|---|
| 166 | |
|---|
| 167 | public Action[] getActions(Object target, Object sender) { |
|---|
| 168 | return actions; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | public void handleAction(Action action, Object sender, Object target) { |
|---|
| 172 | if (action == add) { |
|---|
| 173 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 174 | new EditSkill(EditorPerson.this.viewAdmin, pojo, |
|---|
| 175 | null)); |
|---|
| 176 | } else if (action == edit) { |
|---|
| 177 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 178 | new EditSkill(EditorPerson.this.viewAdmin, pojo, |
|---|
| 179 | target)); |
|---|
| 180 | } else if (action == delete) { |
|---|
| 181 | Session s = HibernateUtil.getSessionFactory() |
|---|
| 182 | .getCurrentSession(); |
|---|
| 183 | s.beginTransaction(); |
|---|
| 184 | |
|---|
| 185 | s.createSQLQuery( |
|---|
| 186 | "DELETE FROM Person_Skill WHERE persons_id = " |
|---|
| 187 | + pojo.getId() + " AND skills_id = " |
|---|
| 188 | + target).executeUpdate(); |
|---|
| 189 | s.createSQLQuery( |
|---|
| 190 | "DELETE FROM Person_skillLevel WHERE Person_id = " |
|---|
| 191 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 192 | + target).executeUpdate(); |
|---|
| 193 | |
|---|
| 194 | pojo = (Person) s.load(Person.class, pojo.getId()); |
|---|
| 195 | s.merge(pojo); |
|---|
| 196 | s.flush(); |
|---|
| 197 | |
|---|
| 198 | updateSkills(); |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | }); |
|---|
| 202 | |
|---|
| 203 | projects.setHeight(100); |
|---|
| 204 | projects.setWidth(600); |
|---|
| 205 | projects.addActionHandler(new Handler() { |
|---|
| 206 | Action add = new Action("add"); |
|---|
| 207 | Action edit = new Action("edit"); |
|---|
| 208 | Action delete = new Action("delete"); |
|---|
| 209 | Action[] actions = { add, edit, delete }; |
|---|
| 210 | |
|---|
| 211 | public Action[] getActions(Object target, Object sender) { |
|---|
| 212 | return actions; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | public void handleAction(Action action, Object sender, Object target) { |
|---|
| 216 | if (action == add) { |
|---|
| 217 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 218 | new EditProject(EditorPerson.this.viewAdmin, pojo, |
|---|
| 219 | null)); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | else if (action == edit) { |
|---|
| 223 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 224 | new EditProject(EditorPerson.this.viewAdmin, pojo, |
|---|
| 225 | target)); |
|---|
| 226 | } else if (action == delete) { |
|---|
| 227 | Session s = HibernateUtil.getSessionFactory() |
|---|
| 228 | .getCurrentSession(); |
|---|
| 229 | s.beginTransaction(); |
|---|
| 230 | |
|---|
| 231 | s.createSQLQuery( |
|---|
| 232 | "DELETE FROM Person_Project WHERE persons_id = " |
|---|
| 233 | + pojo.getId() + " AND projects_id = " |
|---|
| 234 | + target).executeUpdate(); |
|---|
| 235 | s.createSQLQuery( |
|---|
| 236 | "DELETE FROM Person_projectRole WHERE Person_id = " |
|---|
| 237 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 238 | + target).executeUpdate(); |
|---|
| 239 | s.createSQLQuery( |
|---|
| 240 | "DELETE FROM Person_projectStartDate WHERE Person_id = " |
|---|
| 241 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 242 | + target).executeUpdate(); |
|---|
| 243 | s.createSQLQuery( |
|---|
| 244 | "DELETE FROM Person_projectEndDate WHERE Person_id = " |
|---|
| 245 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 246 | + target).executeUpdate(); |
|---|
| 247 | |
|---|
| 248 | pojo = (Person) s.load(Person.class, pojo.getId()); |
|---|
| 249 | s.merge(pojo); |
|---|
| 250 | s.flush(); |
|---|
| 251 | |
|---|
| 252 | updateProjects(); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | }); |
|---|
| 256 | |
|---|
| 257 | degrees.setHeight(100); |
|---|
| 258 | degrees.setWidth(600); |
|---|
| 259 | degrees.addActionHandler(new Handler() { |
|---|
| 260 | Action add = new Action("add"); |
|---|
| 261 | Action edit = new Action("edit"); |
|---|
| 262 | Action delete = new Action("delete"); |
|---|
| 263 | Action[] actions = { add, edit, delete }; |
|---|
| 264 | |
|---|
| 265 | public Action[] getActions(Object target, Object sender) { |
|---|
| 266 | return actions; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | public void handleAction(Action action, Object sender, Object target) { |
|---|
| 270 | if (action == add) { |
|---|
| 271 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 272 | new EditDegree(EditorPerson.this.viewAdmin, pojo, |
|---|
| 273 | null)); |
|---|
| 274 | } else if (action == edit) { |
|---|
| 275 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 276 | new EditDegree(EditorPerson.this.viewAdmin, pojo, |
|---|
| 277 | target)); |
|---|
| 278 | } else if (action == delete) { |
|---|
| 279 | Session s = HibernateUtil.getSessionFactory() |
|---|
| 280 | .getCurrentSession(); |
|---|
| 281 | s.beginTransaction(); |
|---|
| 282 | |
|---|
| 283 | s.createSQLQuery( |
|---|
| 284 | "DELETE FROM Person_Degree WHERE persons_id = " |
|---|
| 285 | + pojo.getId() + " AND degrees_id = " |
|---|
| 286 | + target).executeUpdate(); |
|---|
| 287 | s.createSQLQuery( |
|---|
| 288 | "DELETE FROM Person_degreeStartDate WHERE Person_id = " |
|---|
| 289 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 290 | + target).executeUpdate(); |
|---|
| 291 | s.createSQLQuery( |
|---|
| 292 | "DELETE FROM Person_degreeEndDate WHERE Person_id = " |
|---|
| 293 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 294 | + target).executeUpdate(); |
|---|
| 295 | s.createSQLQuery( |
|---|
| 296 | "DELETE FROM Person_degreePercentReady WHERE Person_id = " |
|---|
| 297 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 298 | + target).executeUpdate(); |
|---|
| 299 | |
|---|
| 300 | pojo = (Person) s.load(Person.class, pojo.getId()); |
|---|
| 301 | s.merge(pojo); |
|---|
| 302 | s.flush(); |
|---|
| 303 | |
|---|
| 304 | updateDegrees(); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | }); |
|---|
| 308 | |
|---|
| 309 | name.setValue(defaults.getName()); |
|---|
| 310 | cellphone.setValue(defaults.getCellphone()); |
|---|
| 311 | email.setValue(defaults.getEmail()); |
|---|
| 312 | nationality.setValue(defaults.getNationality()); |
|---|
| 313 | birthdate.setValue(defaults.getBirthDate()); |
|---|
| 314 | |
|---|
| 315 | form.addComponent(name); |
|---|
| 316 | form.addComponent(cellphone); |
|---|
| 317 | form.addComponent(email); |
|---|
| 318 | form.addComponent(nationality); |
|---|
| 319 | form.addComponent(birthdate); |
|---|
| 320 | |
|---|
| 321 | // the person needs to be saved before it can link to other tables |
|---|
| 322 | if (defaults.getId() != null) { |
|---|
| 323 | skills.setImmediate(true); |
|---|
| 324 | jobs.setImmediate(true); |
|---|
| 325 | publications.setImmediate(true); |
|---|
| 326 | degrees.setImmediate(true); |
|---|
| 327 | projects.setImmediate(true); |
|---|
| 328 | |
|---|
| 329 | updatePublications(); |
|---|
| 330 | updateDegrees(); |
|---|
| 331 | updateJobs(); |
|---|
| 332 | updateSkills(); |
|---|
| 333 | updateProjects(); |
|---|
| 334 | |
|---|
| 335 | addSkill.addListener(new ClickListener() { |
|---|
| 336 | public void buttonClick(ClickEvent event) { |
|---|
| 337 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 338 | new EditSkill(EditorPerson.this.viewAdmin, pojo, |
|---|
| 339 | null)); |
|---|
| 340 | } |
|---|
| 341 | }); |
|---|
| 342 | |
|---|
| 343 | addPublications.addListener(new ClickListener() { |
|---|
| 344 | public void buttonClick(ClickEvent event) { |
|---|
| 345 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 346 | new EditPublication(EditorPerson.this.viewAdmin, |
|---|
| 347 | pojo)); |
|---|
| 348 | } |
|---|
| 349 | }); |
|---|
| 350 | |
|---|
| 351 | addDegree.addListener(new ClickListener() { |
|---|
| 352 | public void buttonClick(ClickEvent event) { |
|---|
| 353 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 354 | new EditDegree(EditorPerson.this.viewAdmin, pojo, |
|---|
| 355 | null)); |
|---|
| 356 | } |
|---|
| 357 | }); |
|---|
| 358 | |
|---|
| 359 | addJob.addListener(new ClickListener() { |
|---|
| 360 | public void buttonClick(ClickEvent event) { |
|---|
| 361 | EditorPerson.this.viewAdmin.getWindow() |
|---|
| 362 | .addWindow( |
|---|
| 363 | new EditJob(EditorPerson.this.viewAdmin, |
|---|
| 364 | pojo, null)); |
|---|
| 365 | } |
|---|
| 366 | }); |
|---|
| 367 | |
|---|
| 368 | addProject.addListener(new ClickListener() { |
|---|
| 369 | public void buttonClick(ClickEvent event) { |
|---|
| 370 | EditorPerson.this.viewAdmin.getWindow().addWindow( |
|---|
| 371 | new EditProject(EditorPerson.this.viewAdmin, pojo, |
|---|
| 372 | null)); |
|---|
| 373 | } |
|---|
| 374 | }); |
|---|
| 375 | |
|---|
| 376 | form.addComponent(skills); |
|---|
| 377 | form.addComponent(addSkill); // skill button |
|---|
| 378 | form.addComponent(projects); |
|---|
| 379 | form.addComponent(addProject); // project button |
|---|
| 380 | form.addComponent(jobs); |
|---|
| 381 | form.addComponent(addJob); // job button |
|---|
| 382 | form.addComponent(publications); |
|---|
| 383 | form.addComponent(addPublications); // pub button |
|---|
| 384 | form.addComponent(degrees); |
|---|
| 385 | form.addComponent(addDegree); // deg button |
|---|
| 386 | |
|---|
| 387 | OrderedLayout actions = new OrderedLayout( |
|---|
| 388 | OrderedLayout.ORIENTATION_HORIZONTAL); |
|---|
| 389 | save.addListener(this); |
|---|
| 390 | delete.addListener(this); |
|---|
| 391 | |
|---|
| 392 | actions.addComponent(save); |
|---|
| 393 | actions.addComponent(delete); |
|---|
| 394 | |
|---|
| 395 | w.addComponent(form); |
|---|
| 396 | w.addComponent(actions); |
|---|
| 397 | } else { |
|---|
| 398 | setHeight(200); |
|---|
| 399 | setWidth(400); |
|---|
| 400 | |
|---|
| 401 | preSave.addListener(this); |
|---|
| 402 | cancel.addListener(this); |
|---|
| 403 | |
|---|
| 404 | OrderedLayout window = new OrderedLayout( |
|---|
| 405 | OrderedLayout.ORIENTATION_HORIZONTAL); |
|---|
| 406 | window.addComponent(form); |
|---|
| 407 | window.addComponent(preSave); |
|---|
| 408 | window.addComponent(cancel); |
|---|
| 409 | w.addComponent(window); |
|---|
| 410 | } |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | public void buttonClick(ClickEvent event) { |
|---|
| 414 | if (event.getButton() == save || event.getButton() == preSave) { |
|---|
| 415 | pojo.setName((String) name.getValue()); |
|---|
| 416 | pojo.setCellphone((String) cellphone.getValue()); |
|---|
| 417 | pojo.setEmail((String) email.getValue()); |
|---|
| 418 | pojo.setNationality((String) nationality.getValue()); |
|---|
| 419 | pojo.setBirthDate((Date) birthdate.getValue()); |
|---|
| 420 | |
|---|
| 421 | pojo = (Person) HibernateUtil.insertObject(pojo); |
|---|
| 422 | |
|---|
| 423 | // we have inserted the new person, now we probably want to add |
|---|
| 424 | // stuff to the tables |
|---|
| 425 | if (event.getButton() == preSave) { |
|---|
| 426 | viewAdmin.getWindow().addWindow( |
|---|
| 427 | new EditorPerson(pojo, "Edit Person", viewAdmin, sess)); |
|---|
| 428 | } |
|---|
| 429 | viewAdmin.loadEntries(); |
|---|
| 430 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | else if (event.getButton() == delete) { |
|---|
| 434 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 435 | s.beginTransaction(); |
|---|
| 436 | s.createSQLQuery( |
|---|
| 437 | "DELETE FROM Job WHERE employee_id = " + pojo.getId()) |
|---|
| 438 | .executeUpdate(); |
|---|
| 439 | HibernateUtil.deleteObject(pojo); |
|---|
| 440 | viewAdmin.loadEntries(); |
|---|
| 441 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | else if (event.getButton() == cancel) { |
|---|
| 445 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 446 | } |
|---|
| 447 | } |
|---|
| 448 | |
|---|
| 449 | public void updatePublications() { |
|---|
| 450 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 451 | s.beginTransaction(); |
|---|
| 452 | |
|---|
| 453 | if (publications.getContainerPropertyIds().size() == 0) { |
|---|
| 454 | publications.addContainerProperty("Publication Title", |
|---|
| 455 | String.class, ""); |
|---|
| 456 | } else { |
|---|
| 457 | publications.removeAllItems(); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | SQLQuery query = s |
|---|
| 461 | .createSQLQuery("SELECT Publication.id AS id, Publication.title AS title FROM Person_Publication " |
|---|
| 462 | + "LEFT JOIN Publication ON Publication.id = Person_Publication.publications_id " |
|---|
| 463 | + "WHERE Person_Publication.authors_id = " |
|---|
| 464 | + pojo.getId()); |
|---|
| 465 | |
|---|
| 466 | for (Object o : query.list()) { |
|---|
| 467 | Object[] o_ = (Object[]) o; |
|---|
| 468 | publications.addItem(new Object[] { o_[1] }, o_[0]); |
|---|
| 469 | } |
|---|
| 470 | |
|---|
| 471 | publications.childRequestedRepaint(null); |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | public void updateJobs() { |
|---|
| 475 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 476 | s.beginTransaction(); |
|---|
| 477 | |
|---|
| 478 | if (jobs.getContainerPropertyIds().size() == 0) { |
|---|
| 479 | jobs.addContainerProperty("Job Title", String.class, ""); |
|---|
| 480 | jobs.addContainerProperty("Employer", String.class, ""); |
|---|
| 481 | jobs.addContainerProperty("Started at", Date.class, null); |
|---|
| 482 | jobs.addContainerProperty("Ended at", Date.class, null); |
|---|
| 483 | } else { |
|---|
| 484 | jobs.removeAllItems(); |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | SQLQuery query = s |
|---|
| 488 | .createSQLQuery("SELECT id, title, employer, startDate, endDate FROM Job WHERE employee_id = " |
|---|
| 489 | + pojo.getId()); |
|---|
| 490 | |
|---|
| 491 | for (Object o : query.list()) { |
|---|
| 492 | Object[] o_ = (Object[]) o; |
|---|
| 493 | jobs.addItem(new Object[] { o_[1], o_[2], o_[3], o_[4] }, o_[0]); |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | } |
|---|
| 497 | |
|---|
| 498 | public void updateDegrees() { |
|---|
| 499 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 500 | |
|---|
| 501 | s.beginTransaction(); |
|---|
| 502 | |
|---|
| 503 | if (degrees.getContainerPropertyIds().size() == 0) { |
|---|
| 504 | degrees.addContainerProperty("Degree name", String.class, ""); |
|---|
| 505 | degrees.addContainerProperty("At institution", String.class, ""); |
|---|
| 506 | degrees.addContainerProperty("Ready %", Integer.class, 0); |
|---|
| 507 | degrees.addContainerProperty("Start Date", String.class, 0); |
|---|
| 508 | degrees.addContainerProperty("Graduation Date", String.class, 0); |
|---|
| 509 | } else { |
|---|
| 510 | degrees.removeAllItems(); |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | SQLQuery query = s |
|---|
| 514 | .createSQLQuery("SELECT D.id AS id, D.title AS _0, D.school AS _1, PDPR.element AS _2, Dstart.element AS _3, Dend.element " |
|---|
| 515 | + "FROM Person_Degree " |
|---|
| 516 | + "LEFT JOIN Degree D ON degrees_id = D.id " |
|---|
| 517 | + "LEFT JOIN Person_degreePercentReady PDPR ON PDPR.mapkey_id = D.id " |
|---|
| 518 | + "LEFT JOIN Person_degreeStartDate Dstart ON Dstart.mapkey_id = D.id " |
|---|
| 519 | + "LEFT JOIN Person_degreeEndDate Dend ON Dend.mapkey_id = D.id " |
|---|
| 520 | + "WHERE persons_id = " |
|---|
| 521 | + pojo.getId() |
|---|
| 522 | + " AND PDPR.Person_id = " |
|---|
| 523 | + pojo.getId() |
|---|
| 524 | + " AND Dstart.Person_id = " |
|---|
| 525 | + pojo.getId() |
|---|
| 526 | + " AND Dend.Person_id = " + pojo.getId()); |
|---|
| 527 | |
|---|
| 528 | for (Object o : query.list()) { |
|---|
| 529 | Object[] o_ = (Object[]) o; |
|---|
| 530 | DateFormat f = SimpleDateFormat |
|---|
| 531 | .getDateInstance(SimpleDateFormat.SHORT); |
|---|
| 532 | degrees.addItem(new Object[] { o_[1], o_[2], o_[3], |
|---|
| 533 | f.format(o_[4]), f.format(o_[5]) }, o_[0]); |
|---|
| 534 | } |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | public void updateProjects() { |
|---|
| 538 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 539 | |
|---|
| 540 | s.beginTransaction(); |
|---|
| 541 | |
|---|
| 542 | if (projects.getContainerPropertyIds().size() == 0) { |
|---|
| 543 | projects.addContainerProperty("Project name", String.class, ""); |
|---|
| 544 | projects.addContainerProperty("Description", String.class, ""); |
|---|
| 545 | projects |
|---|
| 546 | .addContainerProperty("Role in Project", String.class, null); |
|---|
| 547 | projects.addContainerProperty("Started in Project", Date.class, |
|---|
| 548 | null); |
|---|
| 549 | projects.addContainerProperty("Departed Project", Date.class, null); |
|---|
| 550 | } else { |
|---|
| 551 | projects.removeAllItems(); |
|---|
| 552 | } |
|---|
| 553 | |
|---|
| 554 | SQLQuery query = s |
|---|
| 555 | .createSQLQuery("SELECT Project.id, Project.title, Project.description, CONCAT(PProle.element, ''), " |
|---|
| 556 | + "PPstart.element, PPend.element " |
|---|
| 557 | + "FROM Project " |
|---|
| 558 | + "LEFT JOIN Person_Project PP ON Project.id = PP.projects_id " |
|---|
| 559 | + "LEFT JOIN Person_projectRole PPRole ON PPRole.mapkey_id = Project.id " |
|---|
| 560 | + "LEFT JOIN Person_projectStartDate PPstart ON PPstart.mapkey_id = Project.id " |
|---|
| 561 | + "LEFT JOIN Person_projectEndDate PPend ON PPend.mapkey_id = Project.id " |
|---|
| 562 | + "WHERE PP.persons_id = " |
|---|
| 563 | + pojo.getId() |
|---|
| 564 | + " AND PPRole.Person_id = " |
|---|
| 565 | + pojo.getId() |
|---|
| 566 | + " AND PPstart.Person_id = " |
|---|
| 567 | + pojo.getId() |
|---|
| 568 | + " AND PPend.Person_id = " + pojo.getId()); |
|---|
| 569 | |
|---|
| 570 | for (Object o : query.list()) { |
|---|
| 571 | Object[] o_ = (Object[]) o; |
|---|
| 572 | projects.addItem( |
|---|
| 573 | new Object[] { o_[1], o_[2], o_[3], o_[4], o_[5], }, o_[0]); |
|---|
| 574 | } |
|---|
| 575 | } |
|---|
| 576 | |
|---|
| 577 | public void updateSkills() { |
|---|
| 578 | Session s = HibernateUtil.getSessionFactory().getCurrentSession(); |
|---|
| 579 | s.beginTransaction(); |
|---|
| 580 | |
|---|
| 581 | if (skills.getContainerPropertyIds().size() == 0) { |
|---|
| 582 | skills.addContainerProperty("Skill group", String.class, ""); |
|---|
| 583 | skills.addContainerProperty("Skill", String.class, ""); |
|---|
| 584 | skills.addContainerProperty("At level", Integer.class, 0); |
|---|
| 585 | } else { |
|---|
| 586 | skills.removeAllItems(); |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | SQLQuery query = s |
|---|
| 590 | .createSQLQuery("SELECT Skill.id AS id, Skill.grouping AS _0, Skill.name AS _1, Person_skillLevel.element AS _2 " |
|---|
| 591 | + "FROM Person_Skill " |
|---|
| 592 | + "LEFT JOIN Skill ON skills_id = Skill.id " |
|---|
| 593 | + "LEFT JOIN Person_skillLevel ON mapkey_id = Skill.id " |
|---|
| 594 | + "WHERE persons_id = " |
|---|
| 595 | + pojo.getId() |
|---|
| 596 | + "AND Person_skillLevel.Person_id = " + pojo.getId()); |
|---|
| 597 | |
|---|
| 598 | for (Object o : query.list()) { |
|---|
| 599 | Object[] o_ = (Object[]) o; |
|---|
| 600 | skills.addItem(new Object[] { o_[1], o_[2], o_[3] }, o_[0]); |
|---|
| 601 | } |
|---|
| 602 | } |
|---|
| 603 | |
|---|
| 604 | class EditPublication extends Window implements ClickListener { |
|---|
| 605 | private Select select = new Select(); |
|---|
| 606 | private ExpandLayout layout = new ExpandLayout(); |
|---|
| 607 | private FormLayout form = new FormLayout(); |
|---|
| 608 | private OrderedLayout actions = new OrderedLayout(); |
|---|
| 609 | private Button save = new Button("Save", this); |
|---|
| 610 | private Person pojo; |
|---|
| 611 | private ViewAdmin viewAdmin; |
|---|
| 612 | |
|---|
| 613 | public EditPublication(ViewAdmin viewAdmin, Person pojo) { |
|---|
| 614 | super("Publication"); |
|---|
| 615 | this.pojo = pojo; |
|---|
| 616 | this.viewAdmin = viewAdmin; |
|---|
| 617 | |
|---|
| 618 | setHeight(300); |
|---|
| 619 | setWidth(350); |
|---|
| 620 | setLayout(layout); |
|---|
| 621 | layout.addStyleName(Panel.STYLE_LIGHT); |
|---|
| 622 | select.setMultiSelect(true); |
|---|
| 623 | select.setWidth(200); |
|---|
| 624 | |
|---|
| 625 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 626 | .getCurrentSession(); |
|---|
| 627 | sess.beginTransaction(); |
|---|
| 628 | |
|---|
| 629 | for (Object o : sess |
|---|
| 630 | .createSQLQuery( |
|---|
| 631 | "SELECT id, title AS a FROM Publication WHERE title NOT IN " |
|---|
| 632 | + "(SELECT p.title AS title " |
|---|
| 633 | + "FROM Publication AS p " |
|---|
| 634 | + "LEFT JOIN Person_Publication AS pp ON p.id = pp.publications_id " |
|---|
| 635 | + "WHERE authors_id = " + pojo.getId() |
|---|
| 636 | + ") ORDER BY title ASC").list()) { |
|---|
| 637 | Object[] arr = (Object[]) o; |
|---|
| 638 | |
|---|
| 639 | select.addItem(arr[0]); |
|---|
| 640 | select.setItemCaption(arr[0], (String) arr[1]); |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | actions.addComponent(save); |
|---|
| 644 | form.addComponent(select); |
|---|
| 645 | addComponent(form); |
|---|
| 646 | addComponent(actions); |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | public void buttonClick(ClickEvent event) { |
|---|
| 650 | Set<BigInteger> values = (Set<BigInteger>) select.getValue(); |
|---|
| 651 | |
|---|
| 652 | if (event.getButton() == save) { |
|---|
| 653 | if (values.size() == 0) { |
|---|
| 654 | EditorPerson.this.showNotification("Cancelled action", |
|---|
| 655 | Notification.TYPE_TRAY_NOTIFICATION); |
|---|
| 656 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 657 | return; |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 661 | .getCurrentSession(); |
|---|
| 662 | sess.beginTransaction(); |
|---|
| 663 | |
|---|
| 664 | Set<Publication> pubs = pojo.getPublications(); |
|---|
| 665 | |
|---|
| 666 | for (BigInteger id : values) { |
|---|
| 667 | Publication p = (Publication) sess.load(Publication.class, |
|---|
| 668 | id.longValue()); |
|---|
| 669 | // Publication p = new Publication(); |
|---|
| 670 | // p.setId(id.longValue()); |
|---|
| 671 | sess.flush(); |
|---|
| 672 | pubs.add(p); |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | pojo.setPublications(pubs); |
|---|
| 676 | sess.merge(pojo); |
|---|
| 677 | sess.flush(); |
|---|
| 678 | |
|---|
| 679 | updatePublications(); |
|---|
| 680 | viewAdmin.loadEntries(); |
|---|
| 681 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 682 | } |
|---|
| 683 | } |
|---|
| 684 | } |
|---|
| 685 | |
|---|
| 686 | class EditSkill extends Window implements ClickListener { |
|---|
| 687 | private Select select = new Select("Skill"); |
|---|
| 688 | private Select skillLevel = new Select("Skill level"); |
|---|
| 689 | private ExpandLayout layout = new ExpandLayout(); |
|---|
| 690 | private FormLayout form = new FormLayout(); |
|---|
| 691 | private OrderedLayout actions = new OrderedLayout(); |
|---|
| 692 | private Button save = new Button("Save", this); |
|---|
| 693 | private Person pojo; |
|---|
| 694 | private ViewAdmin viewAdmin; |
|---|
| 695 | private Object skillId; |
|---|
| 696 | |
|---|
| 697 | public EditSkill(ViewAdmin viewAdmin, Person pojo, Object skillId) { |
|---|
| 698 | super("Skill & Level"); |
|---|
| 699 | this.pojo = pojo; |
|---|
| 700 | this.viewAdmin = viewAdmin; |
|---|
| 701 | this.skillId = skillId; |
|---|
| 702 | |
|---|
| 703 | setHeight(100); |
|---|
| 704 | setWidth(400); |
|---|
| 705 | setLayout(layout); |
|---|
| 706 | layout.addStyleName(Panel.STYLE_LIGHT); |
|---|
| 707 | |
|---|
| 708 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 709 | .getCurrentSession(); |
|---|
| 710 | sess.beginTransaction(); |
|---|
| 711 | |
|---|
| 712 | if (skillId != null) { |
|---|
| 713 | Object[] skill = (Object[]) sess.createSQLQuery( |
|---|
| 714 | "SELECT grouping, name FROM Skill WHERE id = " |
|---|
| 715 | + skillId).list().get(0); |
|---|
| 716 | select.addItem(skillId); |
|---|
| 717 | select.setItemCaption(skillId, skill[0] + ", " + skill[1]); |
|---|
| 718 | select.setValue(skillId); |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | for (Object o : sess |
|---|
| 722 | .createSQLQuery( |
|---|
| 723 | "SELECT id, grouping, name AS a FROM Skill AS S WHERE id NOT IN " |
|---|
| 724 | + "(SELECT s.id AS id " |
|---|
| 725 | + "FROM Skill AS s " |
|---|
| 726 | + "LEFT JOIN Person_Skill AS ps ON s.id = ps.skills_id " |
|---|
| 727 | + "WHERE persons_id = " + pojo.getId() |
|---|
| 728 | + ") ORDER BY grouping ASC, name ASC") |
|---|
| 729 | .list()) { |
|---|
| 730 | Object[] arr = (Object[]) o; |
|---|
| 731 | |
|---|
| 732 | select.addItem(arr[0]); |
|---|
| 733 | select.setItemCaption(arr[0], arr[1] + ", " + arr[2]); |
|---|
| 734 | } |
|---|
| 735 | |
|---|
| 736 | for (Integer i = 1; i <= 5; i++) { |
|---|
| 737 | skillLevel.addItem(i.toString()); |
|---|
| 738 | } |
|---|
| 739 | skillLevel.setWidth(50); |
|---|
| 740 | |
|---|
| 741 | if (skillId != null) { |
|---|
| 742 | skillLevel.setValue(sess.createSQLQuery( |
|---|
| 743 | "SELECT element FROM Person_skillLevel WHERE mapkey_id = " |
|---|
| 744 | + skillId + " AND Person_id = " + pojo.getId()) |
|---|
| 745 | .uniqueResult().toString()); |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | actions.addComponent(save); |
|---|
| 749 | form.addComponent(select); |
|---|
| 750 | form.addComponent(skillLevel); |
|---|
| 751 | addComponent(form); |
|---|
| 752 | addComponent(actions); |
|---|
| 753 | } |
|---|
| 754 | |
|---|
| 755 | public void buttonClick(ClickEvent event) { |
|---|
| 756 | BigInteger value = (BigInteger) select.getValue(); |
|---|
| 757 | String levelValue = (String) skillLevel.getValue(); |
|---|
| 758 | |
|---|
| 759 | if (event.getButton() == save) { |
|---|
| 760 | if (value == null || levelValue == null) { |
|---|
| 761 | EditorPerson.this.showNotification("Cancelled action", |
|---|
| 762 | Notification.TYPE_TRAY_NOTIFICATION); |
|---|
| 763 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 764 | return; |
|---|
| 765 | } |
|---|
| 766 | |
|---|
| 767 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 768 | .getCurrentSession(); |
|---|
| 769 | sess.beginTransaction(); |
|---|
| 770 | |
|---|
| 771 | Skill s = (Skill) sess.get(Skill.class, value.longValue()); |
|---|
| 772 | |
|---|
| 773 | Set<Skill> skillSet = pojo.getSkills(); |
|---|
| 774 | Map<Skill, Integer> sLevel = pojo.getSkillLevel(); |
|---|
| 775 | |
|---|
| 776 | if (skillId != null) { |
|---|
| 777 | for (Skill o : skillSet) { |
|---|
| 778 | if (o.getId() |
|---|
| 779 | .equals(((BigInteger) skillId).longValue())) { |
|---|
| 780 | |
|---|
| 781 | skillSet.remove(o); |
|---|
| 782 | sLevel.remove(o); |
|---|
| 783 | break; |
|---|
| 784 | } |
|---|
| 785 | } |
|---|
| 786 | } |
|---|
| 787 | |
|---|
| 788 | skillSet.add(s); |
|---|
| 789 | sLevel.put(s, new Integer(levelValue)); |
|---|
| 790 | |
|---|
| 791 | pojo.setSkills(skillSet); |
|---|
| 792 | pojo.setSkillLevel(sLevel); |
|---|
| 793 | |
|---|
| 794 | sess.merge(pojo); |
|---|
| 795 | sess.flush(); |
|---|
| 796 | |
|---|
| 797 | updateSkills(); |
|---|
| 798 | viewAdmin.loadEntries(); |
|---|
| 799 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 800 | } |
|---|
| 801 | } |
|---|
| 802 | } |
|---|
| 803 | |
|---|
| 804 | class EditDegree extends Window implements ClickListener { |
|---|
| 805 | private Select select = new Select("Degree"); |
|---|
| 806 | private TextField ready = new TextField("Percent Ready"); |
|---|
| 807 | private DateField started = new DateField("Start Date"); |
|---|
| 808 | private DateField ended = new DateField("Graduation Date"); |
|---|
| 809 | private ExpandLayout layout = new ExpandLayout(); |
|---|
| 810 | private FormLayout form = new FormLayout(); |
|---|
| 811 | private OrderedLayout actions = new OrderedLayout(); |
|---|
| 812 | private Button save = new Button("Save", this); |
|---|
| 813 | private Person pojo; |
|---|
| 814 | private ViewAdmin viewAdmin; |
|---|
| 815 | private Object degreeId; |
|---|
| 816 | |
|---|
| 817 | public EditDegree(ViewAdmin viewAdmin, Person pojo, Object degreeId) { |
|---|
| 818 | super("Degree Info"); |
|---|
| 819 | this.pojo = pojo; |
|---|
| 820 | this.viewAdmin = viewAdmin; |
|---|
| 821 | this.degreeId = degreeId; |
|---|
| 822 | |
|---|
| 823 | setHeight(160); |
|---|
| 824 | setWidth(400); |
|---|
| 825 | setLayout(layout); |
|---|
| 826 | layout.addStyleName(Panel.STYLE_LIGHT); |
|---|
| 827 | started.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 828 | ended.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 829 | started.setWidth(100); |
|---|
| 830 | ended.setWidth(100); |
|---|
| 831 | |
|---|
| 832 | ready.setWidth(30); |
|---|
| 833 | |
|---|
| 834 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 835 | .getCurrentSession(); |
|---|
| 836 | sess.beginTransaction(); |
|---|
| 837 | |
|---|
| 838 | if (degreeId != null) { |
|---|
| 839 | Object[] degree = (Object[]) sess.createSQLQuery( |
|---|
| 840 | "SELECT title, school FROM Degree WHERE id = " |
|---|
| 841 | + degreeId).list().get(0); |
|---|
| 842 | select.addItem(degreeId); |
|---|
| 843 | select.setItemCaption(degreeId, degree[0] + ", " + degree[1]); |
|---|
| 844 | select.setValue(degreeId); |
|---|
| 845 | } |
|---|
| 846 | |
|---|
| 847 | for (Object o : sess |
|---|
| 848 | .createSQLQuery( |
|---|
| 849 | "SELECT id, title, school FROM Degree WHERE id NOT IN " |
|---|
| 850 | + "(" |
|---|
| 851 | + "SELECT degrees_id AS id FROM Person_Degree WHERE persons_id = " |
|---|
| 852 | + pojo.getId() |
|---|
| 853 | + ") ORDER BY title ASC, school ASC") |
|---|
| 854 | .list()) { |
|---|
| 855 | Object[] arr = (Object[]) o; |
|---|
| 856 | |
|---|
| 857 | select.addItem(arr[0]); |
|---|
| 858 | select.setItemCaption(arr[0], arr[1] + ", " + arr[2]); |
|---|
| 859 | } |
|---|
| 860 | |
|---|
| 861 | if (degreeId != null) { |
|---|
| 862 | ready.setValue(sess |
|---|
| 863 | .createSQLQuery( |
|---|
| 864 | "SELECT element FROM Person_degreePercentReady WHERE mapkey_id = " |
|---|
| 865 | + degreeId + " AND Person_id = " |
|---|
| 866 | + pojo.getId()).uniqueResult() |
|---|
| 867 | .toString()); |
|---|
| 868 | started.setValue(sess |
|---|
| 869 | .createSQLQuery( |
|---|
| 870 | "SELECT element FROM Person_degreeStartDate WHERE mapkey_id = " |
|---|
| 871 | + degreeId + " AND Person_id = " |
|---|
| 872 | + pojo.getId()).uniqueResult()); |
|---|
| 873 | ended.setValue(sess |
|---|
| 874 | .createSQLQuery( |
|---|
| 875 | "SELECT element FROM Person_degreeEndDate WHERE mapkey_id = " |
|---|
| 876 | + degreeId + " AND Person_id = " |
|---|
| 877 | + pojo.getId()).uniqueResult()); |
|---|
| 878 | |
|---|
| 879 | } |
|---|
| 880 | |
|---|
| 881 | actions.addComponent(save); |
|---|
| 882 | form.addComponent(select); |
|---|
| 883 | form.addComponent(ready); |
|---|
| 884 | form.addComponent(started); |
|---|
| 885 | form.addComponent(ended); |
|---|
| 886 | addComponent(form); |
|---|
| 887 | addComponent(actions); |
|---|
| 888 | } |
|---|
| 889 | |
|---|
| 890 | public void buttonClick(ClickEvent event) { |
|---|
| 891 | BigInteger value = (BigInteger) select.getValue(); |
|---|
| 892 | String readyValue = (String) ready.getValue(); |
|---|
| 893 | Date startValue = (Date) started.getValue(); |
|---|
| 894 | Date endValue = (Date) ended.getValue(); |
|---|
| 895 | |
|---|
| 896 | if (event.getButton() == save) { |
|---|
| 897 | |
|---|
| 898 | if (value == null || readyValue == null || startValue == null) { |
|---|
| 899 | EditorPerson.this.showNotification("Cancelled action", |
|---|
| 900 | Notification.TYPE_TRAY_NOTIFICATION); |
|---|
| 901 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 902 | return; |
|---|
| 903 | } |
|---|
| 904 | |
|---|
| 905 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 906 | .getCurrentSession(); |
|---|
| 907 | |
|---|
| 908 | Degree d = new Degree(); |
|---|
| 909 | d.setId(value.longValue()); |
|---|
| 910 | |
|---|
| 911 | Set<Degree> degreeSet = pojo.getDegrees(); |
|---|
| 912 | Map<Degree, Integer> dReady = pojo.getDegreePercentReady(); |
|---|
| 913 | Map<Degree, Date> dStart = pojo.getDegreeStartDate(); |
|---|
| 914 | Map<Degree, Date> dEnd = pojo.getDegreeEndDate(); |
|---|
| 915 | |
|---|
| 916 | if (degreeId != null) { |
|---|
| 917 | for (Degree o : degreeSet) { |
|---|
| 918 | if (o.getId().equals( |
|---|
| 919 | ((BigInteger) degreeId).longValue())) { |
|---|
| 920 | degreeSet.remove(o); |
|---|
| 921 | dReady.remove(o); |
|---|
| 922 | dStart.remove(o); |
|---|
| 923 | dEnd.remove(o); |
|---|
| 924 | break; |
|---|
| 925 | } |
|---|
| 926 | } |
|---|
| 927 | } |
|---|
| 928 | |
|---|
| 929 | try { |
|---|
| 930 | dReady.put(d, new Integer(readyValue)); |
|---|
| 931 | degreeSet.add(d); |
|---|
| 932 | dStart.put(d, startValue); |
|---|
| 933 | dEnd.put(d, endValue); |
|---|
| 934 | } catch (Exception e) { |
|---|
| 935 | getWindow().showNotification( |
|---|
| 936 | "Percentages must be numerals", |
|---|
| 937 | Notification.TYPE_ERROR_MESSAGE); |
|---|
| 938 | return; |
|---|
| 939 | } |
|---|
| 940 | |
|---|
| 941 | pojo.setDegrees(degreeSet); |
|---|
| 942 | pojo.setDegreePercentReady(dReady); |
|---|
| 943 | pojo.setDegreeStartDate(dStart); |
|---|
| 944 | pojo.setDegreeEndDate(dEnd); |
|---|
| 945 | |
|---|
| 946 | sess.beginTransaction(); |
|---|
| 947 | sess.merge(pojo); |
|---|
| 948 | sess.flush(); |
|---|
| 949 | |
|---|
| 950 | updateDegrees(); |
|---|
| 951 | viewAdmin.loadEntries(); |
|---|
| 952 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 953 | } |
|---|
| 954 | } |
|---|
| 955 | } |
|---|
| 956 | |
|---|
| 957 | class EditJob extends Window implements ClickListener { |
|---|
| 958 | private TextField title = new TextField("Job Title"); |
|---|
| 959 | private TextField employer = new TextField("Employer"); |
|---|
| 960 | private DateField started = new DateField("Start Date"); |
|---|
| 961 | private DateField ended = new DateField("End Date"); |
|---|
| 962 | private ExpandLayout layout = new ExpandLayout(); |
|---|
| 963 | private FormLayout form = new FormLayout(); |
|---|
| 964 | private OrderedLayout actions = new OrderedLayout(); |
|---|
| 965 | private Button save = new Button("Save", this); |
|---|
| 966 | private Person pojo; |
|---|
| 967 | private ViewAdmin viewAdmin; |
|---|
| 968 | private Object jobId; |
|---|
| 969 | |
|---|
| 970 | public EditJob(ViewAdmin viewAdmin, Person pojo, Object jobId) { |
|---|
| 971 | super("Job"); |
|---|
| 972 | this.pojo = pojo; |
|---|
| 973 | this.viewAdmin = viewAdmin; |
|---|
| 974 | this.jobId = jobId; |
|---|
| 975 | |
|---|
| 976 | setHeight(160); |
|---|
| 977 | setWidth(400); |
|---|
| 978 | setLayout(layout); |
|---|
| 979 | layout.addStyleName(Panel.STYLE_LIGHT); |
|---|
| 980 | started.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 981 | ended.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 982 | started.setWidth(100); |
|---|
| 983 | ended.setWidth(100); |
|---|
| 984 | |
|---|
| 985 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 986 | .getCurrentSession(); |
|---|
| 987 | sess.beginTransaction(); |
|---|
| 988 | |
|---|
| 989 | if (jobId != null) { |
|---|
| 990 | Object[] job = (Object[]) sess.createSQLQuery( |
|---|
| 991 | "SELECT title, employer, startDate, endDate FROM Job WHERE id = " |
|---|
| 992 | + jobId).list().get(0); |
|---|
| 993 | title.setValue(job[0]); |
|---|
| 994 | employer.setValue(job[1]); |
|---|
| 995 | started.setValue(job[2]); |
|---|
| 996 | ended.setValue(job[3]); |
|---|
| 997 | } |
|---|
| 998 | |
|---|
| 999 | actions.addComponent(save); |
|---|
| 1000 | form.addComponent(title); |
|---|
| 1001 | form.addComponent(employer); |
|---|
| 1002 | form.addComponent(started); |
|---|
| 1003 | form.addComponent(ended); |
|---|
| 1004 | addComponent(form); |
|---|
| 1005 | addComponent(actions); |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | public void buttonClick(ClickEvent event) { |
|---|
| 1009 | String titleValue = (String) title.getValue(); |
|---|
| 1010 | String employerValue = (String) employer.getValue(); |
|---|
| 1011 | Date startValue = (Date) started.getValue(); |
|---|
| 1012 | Date endValue = (Date) ended.getValue(); |
|---|
| 1013 | |
|---|
| 1014 | if (event.getButton() == save) { |
|---|
| 1015 | |
|---|
| 1016 | if (titleValue == null || employerValue == null |
|---|
| 1017 | || startValue == null) { |
|---|
| 1018 | EditorPerson.this.showNotification("Cancelled action", |
|---|
| 1019 | Notification.TYPE_TRAY_NOTIFICATION); |
|---|
| 1020 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 1021 | return; |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 1025 | .getCurrentSession(); |
|---|
| 1026 | sess.beginTransaction(); |
|---|
| 1027 | |
|---|
| 1028 | Job job = new Job(); |
|---|
| 1029 | |
|---|
| 1030 | Set<Job> jobSet = pojo.getJobs(); |
|---|
| 1031 | |
|---|
| 1032 | if (jobId != null) { |
|---|
| 1033 | job = (Job) sess.get(Job.class, ((BigInteger) jobId) |
|---|
| 1034 | .longValue()); |
|---|
| 1035 | |
|---|
| 1036 | for (Job o : jobSet) { |
|---|
| 1037 | if (o.getId().equals(((BigInteger) jobId).longValue())) { |
|---|
| 1038 | jobSet.remove(o); |
|---|
| 1039 | break; |
|---|
| 1040 | } |
|---|
| 1041 | } |
|---|
| 1042 | } |
|---|
| 1043 | |
|---|
| 1044 | job.setEmployer(employerValue); |
|---|
| 1045 | job.setTitle(titleValue); |
|---|
| 1046 | job.setStartDate(startValue); |
|---|
| 1047 | job.setEndDate(endValue); |
|---|
| 1048 | job.setEmployee(pojo); |
|---|
| 1049 | |
|---|
| 1050 | jobSet.add(job); |
|---|
| 1051 | |
|---|
| 1052 | pojo.setJobs(jobSet); |
|---|
| 1053 | |
|---|
| 1054 | sess.save(job); |
|---|
| 1055 | sess.flush(); |
|---|
| 1056 | |
|---|
| 1057 | updateJobs(); |
|---|
| 1058 | viewAdmin.loadEntries(); |
|---|
| 1059 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 1060 | } |
|---|
| 1061 | } |
|---|
| 1062 | } |
|---|
| 1063 | |
|---|
| 1064 | class EditProject extends Window implements ClickListener { |
|---|
| 1065 | private Select select = new Select("Project"); |
|---|
| 1066 | private TextField role = new TextField("Role in Project"); |
|---|
| 1067 | private DateField startDate = new DateField("Joined Project"); |
|---|
| 1068 | private DateField endDate = new DateField("Leaved Project"); |
|---|
| 1069 | private ExpandLayout layout = new ExpandLayout(); |
|---|
| 1070 | private FormLayout form = new FormLayout(); |
|---|
| 1071 | private OrderedLayout actions = new OrderedLayout(); |
|---|
| 1072 | private Button save = new Button("Save", this); |
|---|
| 1073 | private Person pojo; |
|---|
| 1074 | private ViewAdmin viewAdmin; |
|---|
| 1075 | private Object projectId; |
|---|
| 1076 | |
|---|
| 1077 | public EditProject(ViewAdmin viewAdmin, Person pojo, Object projectId) { |
|---|
| 1078 | super("Project"); |
|---|
| 1079 | this.pojo = pojo; |
|---|
| 1080 | this.viewAdmin = viewAdmin; |
|---|
| 1081 | this.projectId = projectId; |
|---|
| 1082 | |
|---|
| 1083 | setHeight(150); |
|---|
| 1084 | setWidth(350); |
|---|
| 1085 | setLayout(layout); |
|---|
| 1086 | layout.addStyleName(Panel.STYLE_LIGHT); |
|---|
| 1087 | select.setWidth(200); |
|---|
| 1088 | startDate.setWidth(100); |
|---|
| 1089 | startDate.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 1090 | endDate.setWidth(100); |
|---|
| 1091 | endDate.setResolution(DateField.RESOLUTION_DAY); |
|---|
| 1092 | |
|---|
| 1093 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 1094 | .getCurrentSession(); |
|---|
| 1095 | sess.beginTransaction(); |
|---|
| 1096 | |
|---|
| 1097 | if (projectId != null) { |
|---|
| 1098 | String title = (String) sess.createSQLQuery( |
|---|
| 1099 | "SELECT title FROM Project WHERE id = " + projectId) |
|---|
| 1100 | .uniqueResult(); |
|---|
| 1101 | select.addItem(projectId); |
|---|
| 1102 | select.setItemCaption(projectId, title); |
|---|
| 1103 | } |
|---|
| 1104 | |
|---|
| 1105 | for (Object o : sess |
|---|
| 1106 | .createSQLQuery( |
|---|
| 1107 | "SELECT id, title FROM Project WHERE id NOT IN " |
|---|
| 1108 | + "(SELECT projects_id AS id FROM Person_Project WHERE persons_id = " |
|---|
| 1109 | + pojo.getId() + ") ORDER BY title ASC") |
|---|
| 1110 | .list()) { |
|---|
| 1111 | Object[] arr = (Object[]) o; |
|---|
| 1112 | |
|---|
| 1113 | select.addItem(arr[0]); |
|---|
| 1114 | select.setItemCaption(arr[0], (String) arr[1]); |
|---|
| 1115 | } |
|---|
| 1116 | |
|---|
| 1117 | actions.addComponent(save); |
|---|
| 1118 | |
|---|
| 1119 | if (projectId != null) { |
|---|
| 1120 | select.setValue(projectId); |
|---|
| 1121 | role.setValue(sess.createSQLQuery( |
|---|
| 1122 | "SELECT element FROM Person_projectRole WHERE Person_id = " |
|---|
| 1123 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 1124 | + projectId).uniqueResult()); |
|---|
| 1125 | startDate.setValue(sess.createSQLQuery( |
|---|
| 1126 | "SELECT element FROM Person_projectStartDate WHERE Person_id = " |
|---|
| 1127 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 1128 | + projectId).uniqueResult()); |
|---|
| 1129 | endDate.setValue(sess.createSQLQuery( |
|---|
| 1130 | "SELECT element FROM Person_projectEndDate WHERE Person_id = " |
|---|
| 1131 | + pojo.getId() + " AND mapkey_id = " |
|---|
| 1132 | + projectId).uniqueResult()); |
|---|
| 1133 | } |
|---|
| 1134 | |
|---|
| 1135 | form.addComponent(select); |
|---|
| 1136 | form.addComponent(role); |
|---|
| 1137 | form.addComponent(startDate); |
|---|
| 1138 | form.addComponent(endDate); |
|---|
| 1139 | |
|---|
| 1140 | addComponent(form); |
|---|
| 1141 | addComponent(actions); |
|---|
| 1142 | } |
|---|
| 1143 | |
|---|
| 1144 | public void buttonClick(ClickEvent event) { |
|---|
| 1145 | BigInteger idValue = (BigInteger) select.getValue(); |
|---|
| 1146 | String roleValue = (String) role.getValue(); |
|---|
| 1147 | Date startValue = (Date) startDate.getValue(); |
|---|
| 1148 | Date endValue = (Date) endDate.getValue(); |
|---|
| 1149 | |
|---|
| 1150 | if (event.getButton() == save) { |
|---|
| 1151 | if (idValue == null || roleValue == null) { |
|---|
| 1152 | EditorPerson.this.showNotification("Cancelled action", |
|---|
| 1153 | Notification.TYPE_TRAY_NOTIFICATION); |
|---|
| 1154 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 1155 | return; |
|---|
| 1156 | } |
|---|
| 1157 | |
|---|
| 1158 | Session sess = HibernateUtil.getSessionFactory() |
|---|
| 1159 | .getCurrentSession(); |
|---|
| 1160 | sess.beginTransaction(); |
|---|
| 1161 | |
|---|
| 1162 | Project p = (Project) sess.get(Project.class, idValue |
|---|
| 1163 | .longValue()); |
|---|
| 1164 | |
|---|
| 1165 | Set<Project> projectSet = pojo.getProjects(); |
|---|
| 1166 | Map<Project, String> projectRoles = pojo.getProjectRole(); |
|---|
| 1167 | Map<Project, Date> projectStart = pojo.getProjectStartDate(); |
|---|
| 1168 | Map<Project, Date> projectEnd = pojo.getProjectEndDate(); |
|---|
| 1169 | |
|---|
| 1170 | if (projectId != null) { |
|---|
| 1171 | for (Project o : projectSet) { |
|---|
| 1172 | if (o.getId().equals( |
|---|
| 1173 | ((BigInteger) projectId).longValue())) { |
|---|
| 1174 | |
|---|
| 1175 | projectSet.remove(o); |
|---|
| 1176 | projectRoles.remove(o); |
|---|
| 1177 | projectStart.remove(o); |
|---|
| 1178 | projectEnd.remove(o); |
|---|
| 1179 | break; |
|---|
| 1180 | } |
|---|
| 1181 | } |
|---|
| 1182 | } |
|---|
| 1183 | |
|---|
| 1184 | projectSet.add(p); |
|---|
| 1185 | projectRoles.put(p, roleValue); |
|---|
| 1186 | projectStart.put(p, startValue); |
|---|
| 1187 | projectEnd.put(p, endValue); |
|---|
| 1188 | |
|---|
| 1189 | pojo.setProjectRole(projectRoles); |
|---|
| 1190 | pojo.setProjectStartDate(projectStart); |
|---|
| 1191 | pojo.setProjectEndDate(projectEnd); |
|---|
| 1192 | sess.merge(pojo); |
|---|
| 1193 | sess.flush(); |
|---|
| 1194 | |
|---|
| 1195 | updateProjects(); |
|---|
| 1196 | viewAdmin.loadEntries(); |
|---|
| 1197 | viewAdmin.getWindow().removeWindow(this); |
|---|
| 1198 | } |
|---|
| 1199 | } |
|---|
| 1200 | } |
|---|
| 1201 | } |
|---|