BrightSide Workbench Full Report + Source Code
PublicDocumentsVM.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2023 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.contacts.profile;
20 
21 import java.io.IOException;
22 import java.util.Collections;
23 import java.util.List;
24 import org.turro.auth.Authentication;
25 import org.turro.config.Configurator;
26 import org.turro.elephant.util.Messages;
27 import org.turro.entities.StandardFolder;
28 import org.turro.file.Document;
29 import org.turro.file.FileWrapper;
30 import org.turro.file.Folder;
31 import org.turro.log.WebLoggers;
32 import org.turro.upload.Medias;
33 import org.zkoss.bind.BindUtils;
34 import org.zkoss.bind.annotation.BindingParam;
35 import org.zkoss.bind.annotation.Command;
36 import org.zkoss.bind.annotation.Init;
37 import org.zkoss.bind.annotation.NotifyChange;
38 import org.zkoss.util.media.Media;
39 
44 public class PublicDocumentsVM {
45 
46  private Folder publicFolder;
47  private long maxDocuments;
48 
49  @Init
50  public void init() {
51  publicFolder = StandardFolder.publishable()
52  .real()
53  .entity(Authentication.getIContact())
54  .folder("public")
55  .asFolder();
56  maxDocuments = Configurator.instance().asLong("Profile.Documents.Maximum");
57  }
58 
59  @NotifyChange("model")
60  @Command("upload")
61  public void upload(@BindingParam("medias") Media medias[]) {
62  if(publicFolder != null) {
63  long count = publicFolder.count();
64  for(Media media : medias) {
65  if(count < maxDocuments) {
66  Medias.toFolder(media, publicFolder);
67  count++;
68  }
69  }
70  }
71  }
72 
73  @Command("download")
74  public void download(@BindingParam("document") Document document) {
75  new FileWrapper(document.document()).download();
76  }
77 
78  @Command("delete")
79  public void deleteDoc(@BindingParam("document") Document document) {
80  Messages.confirmDeletion().add(document.name()).show(() -> {
81  document.delete();
82  BindUtils.postNotifyChange(null, null, PublicDocumentsVM.this, "model");
83  });
84  }
85 
86  public long getMaxDocuments() {
87  return maxDocuments;
88  }
89 
90  public List<Document> getModel() {
91  try {
92  if(publicFolder != null && publicFolder.exists()) return publicFolder.documents();
93  } catch (IOException ex) {
94  WebLoggers.severe(this).exception(ex).log();
95  }
96  return Collections.EMPTY_LIST;
97  }
98 
99 }
void upload(@BindingParam("medias") Media medias[])
void download(@BindingParam("document") Document document)
void deleteDoc(@BindingParam("document") Document document)
static Messages confirmDeletion()
Definition: Messages.java:87
Messages add(String word)
Definition: Messages.java:50
static Folder publishable(String entityPath)
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29
static void toFolder(Media media, Folder folder)
Definition: Medias.java:50