source: ether_statistics/service/implementation/com/medias/megapoli/struts/actions/NewSetAction.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 7.1 KB
Line 
1/*
2 * Created on 10 févr. 2005
3 *
4 * TODO To change the template for this generated file go to
5 * Window - Preferences - Java - Code Style - Code Templates
6 */
7package com.medias.megapoli.struts.actions;
8
9import java.io.FileInputStream;
10import java.lang.reflect.Method;
11import java.util.HashMap;
12import java.util.Map;
13import java.util.Properties;
14
15import javax.servlet.ServletException;
16import javax.servlet.http.HttpServletRequest;
17import javax.servlet.http.HttpServletResponse;
18
19import com.medias.annuaire.Annuaire;
20import com.medias.annuaire.Personne;
21import com.medias.xml.hierarchie.Hierarchie;
22import com.medias.xml.metadata.Metadata;
23import com.medias.xml.metadata.Presentation;
24import com.medias.megapoli.struts.forms.MetadataForm;
25import com.medias.Context;
26import com.medias.mail.Mail;
27import com.medias.mail.MailFactory;
28
29import org.apache.struts.action.Action;
30import org.apache.struts.action.ActionError;
31import org.apache.struts.action.ActionErrors;
32import org.apache.struts.action.ActionForm;
33import org.apache.struts.action.ActionForward;
34import org.apache.struts.action.ActionMapping;
35import org.apache.struts.action.ActionMessage;
36import org.apache.struts.action.ActionMessages;
37
38/**
39 * @author combaz
40 *
41 * TODO To change the template for this generated type comment go to
42 * Window - Preferences - Java - Code Style - Code Templates
43 */
44public class NewSetAction extends Action {
45       
46        public ActionForward execute(ActionMapping mapping,ActionForm form,
47                        HttpServletRequest request, HttpServletResponse response)
48                throws ServletException {
49                String target = "failure";
50               
51                ActionErrors errors = new ActionErrors();
52                ActionMessages mess = new ActionMessages();
53               
54                if(this.isCancelled(request))
55                        target = "cancel";
56               
57                else if(form!=null) {
58                        MetadataForm mform = (MetadataForm)form;
59                        Metadata metadata = mform.getMetadata();
60                       
61                        //on récupÚre le message indiquant le traitement attendu (ajout ou suppression d'un contact)
62                        String message = mform.getMessText();
63                        HashMap<String, String> map = (HashMap<String, String>)this.getKeyMethodMap();
64                        String methodName = map.get(message);
65
66                        //et si le message en question est non nul, on exécute la méthode correspondante.
67                        if(methodName!=null) {
68                                try {
69                                        Class<?>[] paramClasses = null;
70                                        Object[] params = null;
71                                        if(methodName.startsWith("remove")) {
72                                                paramClasses = new Class[1];
73                                                Integer idx = mform.getMessIdx();
74                                                paramClasses[0] = idx.getClass();
75                                                params = new Object[1];
76                                                params[0] = idx;
77                                        }
78                                       
79                                        Presentation pres = metadata.getPres();
80                                        Method method = pres.getClass().getMethod(methodName, paramClasses);
81                                        method.invoke(pres,params);
82                                        target="input";
83                                }
84                                catch(Exception e) {
85                                        System.out.println ("exception2_newSetAction");
86                                        errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.newSet.failed",Context.getWebmaster(request)));
87                                        e.printStackTrace();
88                                }
89                        }
90                       
91                        else {
92                                Hierarchie tree = (Hierarchie) this.getServlet().getServletContext().getAttribute("APP_UPLOADTREE");
93                                String setPath = metadata.getRelativePath();
94
95                                //Si le jeu n'existe pas encore, on le crée...
96                                if(!tree.isFolder(setPath)) {
97                                        try {
98                                                //on remet à jour la métadonnée
99                                                switch (metadata.updateContacts()) {
100                                                        case Metadata.CONTACT_PI:
101                                                                errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.updateMetadata.failed.contact_pi"));
102                                                                saveErrors(request, errors);
103                                                                target = "failure";
104                                                                return mapping.findForward(target);
105                                                        case Metadata.CONTACT_TECH:
106                                                                errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.updateMetadata.failed.contact_tech"));
107                                                                saveErrors(request, errors);
108                                                                target = "failure";
109                                                                return mapping.findForward(target);
110                                                        case Metadata.CONTACT_WRONG:
111                                                                errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.updateMetadata.failed.password"));
112                                                                saveErrors(request, errors);
113                                                                target = "failure";
114                                                                return mapping.findForward(target);
115                                                        default:
116                                                }
117                                                //metadata.updateContacts();
118                                                metadata.createNewSet(tree);
119//                                              metadata.createXmlFile();
120                                                target = "success";
121                                        }
122                                        catch(Exception e) {
123                                                System.out.println ("exception1_newSetAction");
124                                                e.printStackTrace();
125                                                errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.newSet.failed",Context.getWebmaster(request)));
126                                        }
127                                        //on met à jour l'annuaire, au cas où de nouveaux contacts aient été créés...
128                                        Annuaire annu = (Annuaire) this.getServlet().getServletContext().getAttribute("APP_ANNUAIRE");
129                                        try {
130                                                int nbPersAPrevenir = annu.update(metadata, mess);
131                                                if (nbPersAPrevenir != 0)
132                                                        mess.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("data.upload.newSet.succeded"));
133                                                else
134                                                        mess.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("data.upload.newSet.succeded2"));
135                                                target = "success";
136                                        }
137                                        catch(Exception e) {
138                                                System.out.println ("exception3_newSetAction");
139                                                errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.newSet.updateAnnu.failed",Context.getWebmaster(request)));
140                                                e.printStackTrace();
141                                        }
142
143                                        //on prépare les mails à envoyer aux contacts
144                                        Personne user = (Personne) request.getSession().getAttribute("SES_USER");
145                                        String exp_nom = user.getPrenom()+" "+user.getNom();
146                                        String exp_mail = user.getMail();
147                                        String webinf = this.getServlet().getServletContext().getRealPath("WEB-INF");
148                                        //on charge le fichier de proprietes
149                                        Properties prop = new Properties ();
150                                        try {
151                                                prop.load (new FileInputStream (webinf + "/megapoli.properties"));
152                                        }
153                                        catch (Exception e) {
154                                                System.out.println ("exception1_megapoliInitialisation");
155                                                e.printStackTrace ();
156                                        }
157                                        mform.initNewSetMails(prop, exp_mail, exp_nom);
158                                        //on remet les messages à zéro,
159                                        mform.initMess();
160                                        //Envoi d'un mail automatique au webmaster
161                                        MailFactory mf = (MailFactory) this.servlet.getServletContext().getAttribute("APP_MAILFACTORY");
162//                                      String from = "megapoli_info@obs-mip.fr";
163                    String from = Context.getWebmaster(request);
164                                        String to = Context.getWebmaster(request);
165                                        String subject = "[MEGAPOLI] Jeu '"+mform.getMetadata().getPres().getNom()+"' cree";
166                                        Mail mail = new Mail(from, to, null, "", subject);
167                                        try {
168                                                mf.sendMail(mail);
169                                        } catch (Exception e) {
170                                                e.printStackTrace();
171                                        }
172                                }
173                                // Sinon, le jeu existe déjà-> erreur
174                                else {
175                                        errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("errors.upload.newSet.failed2",metadata.getPres().getNom()));
176                                }
177
178                        }                       
179                }
180
181                if(!errors.isEmpty()){
182                        saveErrors(request, errors);
183                        target = "failure";
184                }
185
186                if(!mess.isEmpty()){
187                        saveMessages(request, mess);
188                }                       
189                       
190                return mapping.findForward(target);
191        }
192       
193       
194        protected Map<String, String> getKeyMethodMap() {
195              Map<String, String> map = new HashMap<String, String>();
196              map.put("Ajouter un contact", "addContact");
197              map.put("Add a contact", "addContact");
198              map.put("deletePers", "removeContact");
199              return map;
200          }
201}
Note: See TracBrowser for help on using the repository browser.