BrightSide Workbench Full Report + Source Code
DeliverablesVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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.deliverables.model;
20 
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import org.turro.string.Strings;
28 import org.turro.elephant.util.Messages;
29 import org.turro.entities.Entities;
30 import org.turro.entities.IElephantEntity;
31 import org.turro.file.Document;
32 import org.turro.file.FileWrapper;
33 import org.turro.file.Folder;
34 import org.turro.security.ConceptPermission;
35 import org.turro.upload.Medias;
36 import org.zkoss.bind.BindUtils;
37 import org.zkoss.bind.annotation.BindingParam;
38 import org.zkoss.bind.annotation.Command;
39 import org.zkoss.bind.annotation.ExecutionArgParam;
40 import org.zkoss.bind.annotation.Init;
41 import org.zkoss.bind.annotation.NotifyChange;
42 import org.zkoss.util.media.Media;
43 import org.zkoss.zk.ui.Executions;
44 
49 public class DeliverablesVM {
50 
51  private IElephantEntity iee;
52  private boolean canView, canCreate;
53 
54  @Init
55  public void init(@ExecutionArgParam("entityPath") String entityPath) {
56  if(Strings.isBlank(entityPath)) {
57  entityPath = Executions.getCurrent().getParameter("entityPath");
58  }
59  if(!Strings.isBlank(entityPath)) {
60  iee = Entities.getController(entityPath);
61  } else {
62  iee = Entities.emptyController();
63  }
64  prepareValues();
65  }
66 
68  return iee;
69  }
70 
71  public boolean isCanView() {
72  return canView;
73  }
74 
75  public boolean isCanCreate() {
76  return canCreate;
77  }
78 
79  @NotifyChange("model")
80  @Command("upload")
81  public void upload(@BindingParam("medias") Media medias[]) {
82  Folder folder = iee.getDeliverables();
83  if(folder != null) {
84  for(Media media : medias) {
85  Medias.toFolder(media, folder);
86  }
87  }
88  }
89 
90  @Command("download")
91  public void download(@BindingParam("document") Document document) {
92  new FileWrapper(document.document()).download();
93  }
94 
95  @Command("delete")
96  public void deleteDoc(@BindingParam("document") Document document) {
97  Messages.confirmDeletion().add(document.name()).show(() -> {
98  document.delete();
99  BindUtils.postNotifyChange(null, null, DeliverablesVM.this, "model");
100  });
101  }
102 
103  public List<Document> getModel() {
104  try {
105  Folder folder = iee.getDeliverables();
106  if(folder != null && folder.exists()) return folder.documents();
107  } catch (IOException ex) {
108  Logger.getLogger(DeliverablesVM.class.getName()).log(Level.SEVERE, null, ex);
109  }
110  return Collections.EMPTY_LIST;
111  }
112 
113  private void prepareValues() {
114  Optional.ofNullable(iee.getConceptPermission(ConceptPermission.DELIVERABLES))
115  .ifPresentOrElse(permission -> {
116  canCreate = permission.canAct();
117  canView = permission.canShow();
118  }, () -> {
119  canCreate = false;
120  canView = false;
121  });
122  }
123 
124 }
void deleteDoc(@BindingParam("document") Document document)
void upload(@BindingParam("medias") Media medias[])
void init(@ExecutionArgParam("entityPath") String entityPath)
void download(@BindingParam("document") Document document)
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static IElephantEntity getController(String path)
Definition: Entities.java:78
static IElephantEntity emptyController()
Definition: Entities.java:74
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50
ConceptPermission getConceptPermission(String name)