BrightSide Workbench Full Report + Source Code
ProposalSet.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.contacts.proposal;
19 
20 import java.io.File;
21 import java.io.Serializable;
22 import java.util.List;
23 import java.util.TreeSet;
24 import org.turro.action.Plugins;
25 import org.turro.auth.Authentication;
26 import org.turro.command.Command;
27 import org.turro.command.Context;
28 import org.turro.contacts.Contact;
29 import org.turro.elephant.context.Application;
30 import org.turro.elephant.impl.util.Serializer;
31 import org.turro.file.FileWrapper;
32 import org.turro.plugin.IPlugin;
33 import org.turro.plugin.contacts.IContact;
34 
39 public class ProposalSet extends TreeSet<ContactProposal> implements Serializable {
40 
41  private static final long serialVersionUID = 1;
42 
43  public void loadFromVCards(Application app, final Command command) {
44  final IPlugin plugin = Plugins.loadImplementation(IPlugin.class, "vcard");
45  if(plugin != null) {
46  plugin.setData("constructor", app.getConstructor());
47  if(command != null) {
48  FileWrapper fw = new FileWrapper((File) plugin.getData("importFolder"));
49  fw.upload(new Command() {
50  @Override
51  public Object execute(Context context) {
52  addProposals(plugin);
53  command.execute(context);
54  return null;
55  }
56  });
57  return;
58  } else {
59  addProposals(plugin);
60  }
61  }
62  }
63 
64  private void addProposals(IPlugin plugin) {
65  List<Contact> l = (List<Contact>) plugin.getData("import");
67  for(Contact c : l) {
68  add(new ContactProposal(c, "VCard Import [" + ic.getName() + "]"));
69  }
70  l = (List<Contact>) plugin.getData("importcsv");
71  for(Contact c : l) {
72  add(new ContactProposal(c, "CSV Import [" + ic.getName() + "]"));
73  }
74  }
75 
76  public static void serialize(Application app, ProposalSet instance) {
77  if(app.isInRole("contact-proposal:list")) {
78  Serializer.serialize("/proposals/data.ser", instance);
79  } else if(app.isInRole("contact-proposal:import")) {
80  Serializer.serialize("/proposals/imported/" + Authentication.getIContact().getId() + ".ser", instance);
81  }
82  }
83 
84  public static ProposalSet deserialize(Application app) {
85  ProposalSet result = new ProposalSet();
86  if(app.isInRole("contact-proposal:list")) {
87  ProposalSet ps = (ProposalSet) Serializer.deserialize("/proposals/data.ser");
88  if(ps != null) result.addAll(ps);
89  File[] fs = Serializer.getFiles("/proposals/imported");
90  if(fs != null) {
91  for(File f : fs) {
93  if(ps != null) result.addAll(ps);
94  f.delete();
95  }
96  }
97  ProposalSet.serialize(app, result);
98  } else if(app.isInRole("contact-proposal:import")) {
99  ProposalSet ps = (ProposalSet) Serializer.deserialize("/proposals/imported/" + Authentication.getIContact().getId() + ".ser");
100  if(ps != null) result.addAll(ps);
101  }
102  return result;
103  }
104 
105 }
static< T > T loadImplementation(Class< T > jclass)
Definition: Plugins.java:57
static ProposalSet deserialize(Application app)
static void serialize(Application app, ProposalSet instance)
void loadFromVCards(Application app, final Command command)
static void serialize(String fileName, Object instance)
Definition: Serializer.java:49
static Object deserialize(String fileName)
Definition: Serializer.java:60
static File[] getFiles(String rootName)
Definition: Serializer.java:34
void upload(final Command command)
Object getData(String id)
void setData(String id, Object data)