1 /* ===============================================================================2 *3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)4 *5 * ===============================================================================6 *7 * Copyright (C)8 * 9 * This program is free software; you can redistribute it and/or modify it under10 * the terms of the GNU General Public License version 2, as published by the11 * Free Software Foundation. See the file LICENSE.html for more information.12 * 13 * This program is distributed in the hope that it will be useful, but WITHOUT14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.16 * 17 * You should have received a copy of the GNU General Public License along with18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.20 *21 * ===============================================================================22 */23 24 package org.infoglue.cms.treeservice;25 26 27 public class NodeService //extends JServiceBuilder28 {29 /*30 public static final String DatabaseFile = "database.xml";31 public static final String MappingFile = "mapping.xml";32 33 private Mapping mapping;34 private JDO jdo;35 36 37 public void init(ServletConfig config) throws ServletException38 {39 super.init(config);40 41 try42 {43 mapping = new Mapping( getClass().getClassLoader() );44 mapping.loadMapping( getClass().getResource( MappingFile ) );45 46 jdo = new JDO();47 jdo.setConfiguration( getClass().getResource( DatabaseFile ).toString() );48 jdo.setDatabaseName( "maingate" );49 }50 catch(Exception e)51 {52 logger.info("Error trying to initialize db");53 }54 }55 56 57 public CommunicationEnvelope execute(CommunicationEnvelope envelope)58 {59 CommunicationEnvelope responseEnvelope = new CommunicationEnvelope();60 61 try62 { 63 String action = envelope.getAction();64 logger.info("ACTION:" + action);65 66 if(action.equals("selectRootNode"))67 {68 responseEnvelope = getRootNode(envelope);69 }70 else if(action.equals("selectNode"))71 {72 }73 else if(action.equals("createNode"))74 {75 responseEnvelope = createNode(envelope);76 }77 else if(action.equals("updateNode"))78 {79 responseEnvelope = updateNode(envelope);80 }81 else if(action.equals("deleteNode"))82 {83 responseEnvelope = deleteNode(envelope);84 }85 86 logger.info("Executing in NodeService...");87 }88 catch (Exception e)89 {90 responseEnvelope.setStatus(1);91 e.printStackTrace(); 92 }93 return responseEnvelope;94 }95 96 public CommunicationEnvelope getRootNode(CommunicationEnvelope envelope)97 {98 CommunicationEnvelope responseEnvelope = new CommunicationEnvelope();99 100 try101 { 102 Database db = jdo.getDatabase();103 104 db.begin();105 106 Node node = null;107 108 String oqlString = "SELECT n FROM Node n WHERE is_undefined(parent)";109 logger.info("oqlString:" + oqlString);110 OQLQuery oql = db.getOQLQuery(oqlString);111 logger.info("oql prepared");112 QueryResults results = oql.execute();113 logger.info("results fetched");114 if(results.hasMore())115 node = (Node)results.next();116 117 logger.info("Fetched a node:" + node);118 responseEnvelope.setData(node);119 120 db.commit();121 }122 catch (Exception e)123 {124 responseEnvelope.setStatus(1);125 e.printStackTrace(); 126 }127 return responseEnvelope;128 }129 130 131 public CommunicationEnvelope updateNode(CommunicationEnvelope envelope)132 {133 CommunicationEnvelope responseEnvelope = new CommunicationEnvelope();134 135 try136 { 137 Database db = jdo.getDatabase();138 139 db.begin();140 141 Node updatedNode = (Node)envelope.getData();142 logger.info("Node to update:" + updatedNode);143 144 //Kan man ändra så att den sparas direkt kanske?145 Node node = (Node)db.load(Node.class, updatedNode.getId());146 node.setChildren(updatedNode.getChildren());147 node.setName(updatedNode.getName());148 node.setParent(updatedNode.getParent());149 150 logger.info("Executing in NodeService...");151 responseEnvelope.setData(node);152 153 db.commit();154 }155 catch (Exception e)156 {157 responseEnvelope.setStatus(1);158 e.printStackTrace(); 159 }160 return responseEnvelope;161 }162 163 164 public CommunicationEnvelope createNode(CommunicationEnvelope envelope)165 {166 CommunicationEnvelope responseEnvelope = new CommunicationEnvelope();167 168 try169 { 170 Database db = jdo.getDatabase();171 172 db.begin();173 174 Node newNode = (Node)envelope.getData();175 logger.info("Node to create:" + newNode);176 177 //Kan man ändra så att den sparas direkt kanske?178 Node node = new Node();179 node.setName(newNode.getName());180 181 Node parentNode = (Node)db.load(Node.class, newNode.getParent().getId());182 node.setParent(parentNode);183 node.setChildren(newNode.getChildren());184 db.create(node);185 186 logger.info("Executing in NodeService...");187 responseEnvelope.setData(node);188 189 db.commit();190 }191 catch (Exception e)192 {193 responseEnvelope.setStatus(1);194 e.printStackTrace(); 195 }196 return responseEnvelope;197 }198 199 public CommunicationEnvelope deleteNode(CommunicationEnvelope envelope)200 {201 CommunicationEnvelope responseEnvelope = new CommunicationEnvelope();202 203 try204 { 205 Database db = jdo.getDatabase();206 207 db.begin();208 209 Node deleteNode = (Node)envelope.getData();210 Node parent = deleteNode.getParent();211 logger.info("Node to delete:" + deleteNode);212 213 //Kan man ändra så att den sparas direkt kanske?214 Node node = (Node)db.load(Node.class, deleteNode.getId());215 db.remove(node);216 217 logger.info("Executing in NodeService...");218 responseEnvelope.setData(parent);219 220 db.commit();221 }222 catch (Exception e)223 {224 responseEnvelope.setStatus(1);225 e.printStackTrace(); 226 }227 return responseEnvelope;228 }229 230 */231 }