001 package com.croftsoft.apps.agoracast.c2p;
002
003 import java.io.*;
004 import java.util.*;
005
006 import com.croftsoft.core.io.SerializableLib;
007 import com.croftsoft.core.lang.NullArgumentException;
008 import com.croftsoft.core.text.sml.*;
009 // import com.croftsoft.core.util.event.EventBroadcaster;
010 // import com.croftsoft.core.util.event.Event;
011 // import com.croftsoft.core.util.event.EventListener;
012 import com.croftsoft.core.util.SoftHashMap;
013
014 /*********************************************************************
015 *
016 * <p />
017 *
018 * @version
019 * 2001-08-09
020 * @since
021 * 2001-08-06
022 * @author
023 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
024 *********************************************************************/
025
026 public final class AgoracastMemory
027 implements AgoracastDatabase
028 //////////////////////////////////////////////////////////////////////
029 //////////////////////////////////////////////////////////////////////
030 {
031
032 public static final String SML_NODE_NAME = "database";
033
034 // private final EventBroadcaster eventBroadcaster;
035
036 //
037
038 private boolean isDirty;
039
040 private Map idToDataMap;
041
042 //////////////////////////////////////////////////////////////////////
043 //////////////////////////////////////////////////////////////////////
044
045 public static AgoracastMemory load ( String filename )
046 throws IOException
047 //////////////////////////////////////////////////////////////////////
048 {
049 return fromSmlNode ( SmlNodeLib.load ( filename, false ) );
050 }
051
052 public static AgoracastMemory fromSmlNode ( SmlNode smlNode )
053 //////////////////////////////////////////////////////////////////////
054 {
055 if ( !SML_NODE_NAME.equals ( smlNode.getName ( ) ) )
056 {
057 throw new IllegalArgumentException ( smlNode.getName ( ) );
058 }
059
060 Map idToDataMap = new SoftHashMap ( );
061
062 SmlNode [ ] dataNodes
063 = smlNode.getChildNodes ( AgoracastData.SML_NODE_NAME );
064
065 if ( dataNodes != null )
066 {
067 for ( int i = 0; i < dataNodes.length; i++ )
068 {
069 AgoracastData agoracastData
070 = AgoracastData.fromSmlNode ( dataNodes [ i ] );
071
072 idToDataMap.put (
073 agoracastData.getArticleId ( ), agoracastData );
074 }
075 }
076
077 return new AgoracastMemory ( idToDataMap );
078 }
079
080 //////////////////////////////////////////////////////////////////////
081 //////////////////////////////////////////////////////////////////////
082
083 public AgoracastMemory ( Map idToDataMap )
084 //////////////////////////////////////////////////////////////////////
085 {
086 NullArgumentException.check ( this.idToDataMap = idToDataMap );
087
088 // eventBroadcaster = new EventBroadcaster ( );
089 }
090
091 public AgoracastMemory ( )
092 //////////////////////////////////////////////////////////////////////
093 {
094 this ( new SoftHashMap ( ) );
095 }
096
097 //////////////////////////////////////////////////////////////////////
098 //////////////////////////////////////////////////////////////////////
099
100 /*
101 public boolean addEventListener ( EventListener eventListener )
102 //////////////////////////////////////////////////////////////////////
103 {
104 return eventBroadcaster.addEventListener ( eventListener );
105 }
106 */
107
108 //////////////////////////////////////////////////////////////////////
109 //////////////////////////////////////////////////////////////////////
110
111 public synchronized boolean add ( AgoracastData agoracastData )
112 //////////////////////////////////////////////////////////////////////
113 {
114 String articleId = agoracastData.getArticleId ( );
115
116 idToDataMap.put ( articleId, agoracastData );
117
118 isDirty = true;
119
120 // eventBroadcaster.broadcast (
121 // new AgoracastEvent ( AgoracastEvent.TYPE_ADD, articleId ) );
122
123 return true;
124 }
125
126 public AgoracastData getAgoracastData ( String articleId )
127 //////////////////////////////////////////////////////////////////////
128 {
129 return ( AgoracastData ) idToDataMap.get ( articleId );
130 }
131
132 public synchronized AgoracastData [ ] getAgoracastDatas ( )
133 //////////////////////////////////////////////////////////////////////
134 {
135 return ( AgoracastData [ ] )
136 idToDataMap.values ( ).toArray ( new AgoracastData [ ] { } );
137 }
138
139 public synchronized AgoracastData [ ]
140 getAgoracastDatasForCategory ( String category )
141 //////////////////////////////////////////////////////////////////////
142 {
143 Iterator iterator = idToDataMap.values ( ).iterator ( );
144
145 List agoracastDataList = new ArrayList ( );
146
147 while ( iterator.hasNext ( ) )
148 {
149 AgoracastData agoracastData
150 = ( AgoracastData ) iterator.next ( );
151
152 if ( category.equals ( agoracastData.getValue (
153 AgoracastConstants.FIELD_NAME_CATEGORY ) ) )
154 {
155 agoracastDataList.add ( agoracastData );
156 }
157 }
158
159 return ( AgoracastData [ ] )
160 agoracastDataList.toArray ( new AgoracastData [ ] { } );
161 }
162
163 //////////////////////////////////////////////////////////////////////
164 //////////////////////////////////////////////////////////////////////
165
166 public synchronized boolean saveIfDirty ( String filename )
167 throws IOException
168 //////////////////////////////////////////////////////////////////////
169 {
170 if ( isDirty )
171 {
172 SmlNodeLib.save ( filename, toSmlNode ( ) );
173
174 isDirty = false;
175
176 return true;
177 }
178
179 return false;
180 }
181
182 public synchronized SmlNode toSmlNode ( )
183 //////////////////////////////////////////////////////////////////////
184 {
185 SmlNode rootSmlNode = new SmlNode ( SML_NODE_NAME );
186
187 if ( idToDataMap != null )
188 {
189 Iterator iterator = idToDataMap.values ( ).iterator ( );
190
191 while ( iterator.hasNext ( ) )
192 {
193 AgoracastData agoracastData
194 = ( AgoracastData ) iterator.next ( );
195
196 rootSmlNode.add ( agoracastData.toSmlNode ( ) );
197 }
198 }
199
200 return rootSmlNode;
201 }
202
203 //////////////////////////////////////////////////////////////////////
204 //////////////////////////////////////////////////////////////////////
205 }