Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
915 migan 1
import java.io.BufferedReader;
2
import java.io.FileReader;
3
import java.io.PrintWriter;
4
import java.text.SimpleDateFormat;
5
import java.util.ArrayList;
6
import java.util.Date;
7
import java.util.Iterator;
8
 
9
import items.*;
10
 
11
public class Database {
12
    private myXML rootXML;
13
    private String filename;
14
    private boolean hasChanged;
15
    private String changeLog;
16
 
17
    public Database(String filename) throws Exception
18
    {
19
        this.filename = filename;
20
 
21
        FileReader fr = new FileReader(this.filename);
22
 
23
        BufferedReader bf = new BufferedReader(fr);
24
 
25
        this.rootXML = new myXML(bf);
26
 
27
        bf.close();
28
 
29
        fr.close();
30
 
31
        this.hasChanged = false;
32
        this.changeLog = "";
33
    }
34
 
35
    public boolean hasChanged()
36
    {
37
        return this.hasChanged;
38
    }
39
 
40
    public String getChangeLog()
41
    {
42
        return this.changeLog;
43
    }
44
 
45
    public void saveChanges() throws Exception
46
    {
47
        this.saveChanges(this.filename);
48
    }
49
 
50
    public void saveChanges(String filename) throws Exception
51
    {
52
        SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss");
53
        String formattedDate = formatter.format(new Date());
54
 
55
        this.rootXML.setComment("Written by CanIdManager on " + formattedDate);
56
 
57
        PrintWriter pw = new PrintWriter(filename);
58
 
59
        this.rootXML.serialize(pw);
60
 
61
        pw.close();
62
    }
63
 
64
    /* =================== */
65
    /* == Class methods == */
66
    /* =================== */
67
 
68
    public CanClass getClassByName(String name)
69
    {
70
        ArrayList<CanClass> classes = this.getClasses();
71
 
72
        Iterator<CanClass> iterator = classes.iterator();
73
 
74
        while (iterator.hasNext())
75
        {
76
            CanClass canClass = iterator.next();
77
 
78
            if (canClass.getName().compareTo(name) == 0)
79
            {
80
                return canClass;
81
            }
82
        }
83
 
84
        return null;
85
    }
86
 
87
    public ArrayList<CanClass> getClasses()
88
    {
89
        ArrayList<CanClass> classes = new ArrayList<CanClass>();
90
 
91
        myXML elementClasses = this.rootXML.findElement("root").findElement("classes");
92
 
93
        for (int n = 0; n < elementClasses.size(); n++)
94
        {
95
            myXML elementClass = elementClasses.getElement(n);
96
 
97
            CanClass canClass = new CanClass();
98
            canClass.setId(Integer.parseInt(elementClass.Attribute.find("id")));
99
            canClass.setName(elementClass.Attribute.find("name"));
100
            canClass.setDescription(elementClass.Attribute.find("description"));
101
 
102
            classes.add(canClass);
103
        }
104
 
105
        return classes;
106
    }
107
 
108
    public int getNewClassId(CanClass inputCanClass)
109
    {
110
        ArrayList<Integer> takenIds = new ArrayList<Integer>();
111
 
112
        Iterator<CanClass> iterator = this.getClasses().iterator();
113
 
114
        while (iterator.hasNext())
115
        {
116
            takenIds.add(iterator.next().getId());
117
        }
118
 
119
        int id = 0;
120
 
121
        while (takenIds.contains(id))
122
        {
123
            id++;
124
        }
125
 
126
        if (id > 32)
127
        {
128
            System.err.println("Warning: No more ids are available for this classes");
129
        }
130
 
131
        return id;
132
    }
133
 
134
    public void addClass(CanClass canClass) throws Exception
135
    {
136
        myXML elementClasses = this.rootXML.findElement("root").findElement("classes");
137
 
138
        myXML elementClass = elementClasses.addElement("class");
139
        elementClass.Attribute.add("id", canClass.getId());
140
        elementClass.Attribute.add("name", canClass.getName());
141
        elementClass.Attribute.add("description", canClass.getDescription());
142
 
143
        this.hasChanged = true;
144
        this.changeLog += "Added new class: " + canClass.getName() + " with id " + canClass.getId() + "\n";
145
    }
146
 
147
    /* ===================== */
148
    /* == Command methods == */
149
    /* ===================== */
150
 
151
    public Command getCommandByName(String name)
152
    {
153
        ArrayList<Command> commands = this.getCommands();
154
 
155
        Iterator<Command> iterator = commands.iterator();
156
 
157
        while (iterator.hasNext())
158
        {
159
            Command command = iterator.next();
160
 
161
            if (command.getName().compareTo(name) == 0)
162
            {
163
                return command;
164
            }
165
        }
166
 
167
        return null;
168
    }
169
 
170
    public ArrayList<Command> getCommands()
171
    {
172
        ArrayList<Command> commands = new ArrayList<Command>();
173
 
174
        myXML elementCommands = this.rootXML.findElement("root").findElement("commands");
175
 
176
        for (int n = 0; n < elementCommands.size(); n++)
177
        {
178
            myXML elementCommand = elementCommands.getElement(n);
179
 
180
            Command command = new Command();
181
            command.setId(Integer.parseInt(elementCommand.Attribute.find("id")));
182
            command.setName(elementCommand.Attribute.find("name"));
183
            command.setDescription(elementCommand.Attribute.find("description"));
184
 
185
            String type = elementCommand.Attribute.find("type");
186
 
187
            CommandType commandType = this.getCommandType(type);
188
 
189
            if (commandType == null)
190
            {
191
                System.err.println("Warning: Found command with invalid command type: Name=" + command.getName() + ", Type="+type);
192
            }
193
 
194
            command.setType(commandType);
195
 
196
            commands.add(command);
197
        }
198
 
199
        return commands;
200
    }
201
 
202
    public int getNewCommandId(Command inputCommand)
203
    {
204
        ArrayList<Integer> takenIds = new ArrayList<Integer>();
205
 
206
        Iterator<Command> iterator = this.getCommands().iterator();
207
 
208
        while (iterator.hasNext())
209
        {
210
            Command command = iterator.next();
211
 
212
            // First check if the type is the same, if it is maybe there is an id taken
213
            if (command.getType().getName().compareTo(inputCommand.getType().getName()) == 0)
214
            {
215
                // Then check if type is module, then we must also check module name
216
                if (inputCommand.getType().getName().compareTo("module") == 0)
217
                {
218
                    // Check if module name is the same, then we have an id taken
219
                    if (command.getModule().compareTo(inputCommand.getModule()) == 0)
220
                    {
221
                        takenIds.add(command.getId());
222
                    }
223
                }
224
                else
225
                {
226
                    takenIds.add(command.getId());
227
                }
228
            }
229
        }
230
 
231
        int id = inputCommand.getType().getMinId();
232
 
233
        while (takenIds.contains(id))
234
        {
235
            id++;
236
        }
237
 
238
        if (id > inputCommand.getType().getMaxId())
239
        {
240
            System.err.println("Warning: No more ids are available for this type: " + inputCommand.getType().getName());
241
        }
242
 
243
        return id;
244
    }
245
 
246
    public void addCommand(Command command) throws Exception
247
    {
248
        myXML elementCommands = this.rootXML.findElement("root").findElement("commands");
249
 
250
        myXML elementCommand = elementCommands.addElement("command");
251
        elementCommand.Attribute.add("id", command.getId());
252
        elementCommand.Attribute.add("name", command.getName());
253
        elementCommand.Attribute.add("type", command.getType().getName());
254
        elementCommand.Attribute.add("module", command.getModule());
255
        elementCommand.Attribute.add("description", command.getDescription());
256
 
257
        this.hasChanged = true;
258
        this.changeLog += "Added new command: " + command.getName() + " with id " + command.getId() + "\n";
259
    }
260
 
261
    /* ==================== */
262
    /* == Module methods == */
263
    /* ==================== */
264
 
265
    public Module getModuleByName(String name)
266
    {
267
        ArrayList<Module> modules = this.getModules();
268
 
269
        Iterator<Module> iterator = modules.iterator();
270
 
271
        while (iterator.hasNext())
272
        {
273
            Module module = iterator.next();
274
 
275
            if (module.getName().compareTo(name) == 0)
276
            {
277
                return module;
278
            }
279
        }
280
 
281
        return null;
282
    }
283
 
284
    public ArrayList<Module> getModules()
285
    {
286
        ArrayList<Module> modules = new ArrayList<Module>();
287
 
288
        myXML elementClasses = this.rootXML.findElement("root").findElement("classes");
289
 
290
        for (int n = 0; n < elementClasses.size(); n++)
291
        {
292
            myXML elementClass = elementClasses.getElement(n);
293
 
294
            CanClass canClass = new CanClass();
295
            canClass.setId(Integer.parseInt(elementClass.Attribute.find("id")));
296
            canClass.setName(elementClass.Attribute.find("name"));
297
            canClass.setDescription(elementClass.Attribute.find("description"));
298
 
299
            myXML elementModules = elementClass.findElement("modules");
300
 
301
            if (elementModules == null)
302
            {
303
                continue;
304
            }
305
 
306
            for (int m = 0; m < elementModules.size(); m++)
307
            {
308
                myXML elementModule = elementModules.getElement(m);
309
 
310
                Module module = new Module();
311
                module.setId(Integer.parseInt(elementModule.Attribute.find("id")));
312
                module.setName(elementModule.Attribute.find("name"));
313
                module.setDescription(elementModule.Attribute.find("description"));
314
                module.setCanClass(canClass);
315
 
316
                myXML elementSupportedCommands = elementModule.findElement("supportedCommands");
317
 
318
                if (elementSupportedCommands == null)
319
                {
320
                    continue;
321
                }
322
 
323
                for (int s = 0; s < elementSupportedCommands.size(); s++)
324
                {
325
                    myXML elementSupportedCommand = elementSupportedCommands.getElement(s);
326
 
327
                    SupportedCommand supportedCommand = new SupportedCommand();
328
                    supportedCommand.setName(elementSupportedCommand.Attribute.find("name"));
329
 
330
                    DirectionType directionType = this.getDirectionType(elementSupportedCommand.Attribute.find("direction"));
331
 
332
                    if (directionType == null)
333
                    {
334
                        System.err.println("Warning: Direction type is invalid for supported command: " + supportedCommand.toString());
335
                    }
336
 
337
                    supportedCommand.setDirection(directionType);
338
 
339
                    myXML elementSendingTypes = elementSupportedCommand.findElement("sendingTypes");
340
 
341
                    if (elementSendingTypes == null)
342
                    {
343
                        continue;
344
                    }
345
 
346
                    for (int t = 0; t < elementSendingTypes.size(); t++)
347
                    {
348
                        myXML elementSendingType = elementSendingTypes.getElement(t);
349
 
350
                        SendingType sendingType = this.getSendingType(elementSendingType.Attribute.find("name"));
351
 
352
                        if (sendingType == null)
353
                        {
354
                            System.err.println("Warning: Sending type is invalid for supported command: " + supportedCommand.toString());
355
                        }
356
 
357
                        supportedCommand.addSendingTypes(sendingType);
358
                    }
359
 
360
                    supportedCommand.setModule(module);
361
                    supportedCommand.setCanClass(canClass);
362
 
363
                    module.addSupportedCommand(supportedCommand);
364
                }
365
 
366
                modules.add(module);
367
            }
368
        }      
369
 
370
        return modules;
371
    }
372
 
373
    public int getNewModuleId(Module inputModule)
374
    {
375
        ArrayList<Integer> takenIds = new ArrayList<Integer>();
376
 
377
        Iterator<Module> iterator = this.getModules().iterator();
378
 
379
        while (iterator.hasNext())
380
        {
381
            Module module = iterator.next();
382
 
383
            // First check if the class is the same, if it is maybe there is an id taken
384
            if (module.getClass().getName().compareTo(inputModule.getClass().getName()) == 0)
385
            {
386
                takenIds.add(module.getId());
387
            }
388
        }
389
 
390
        int id = 0;
391
 
392
        while (takenIds.contains(id))
393
        {
394
            id++;
395
        }
396
 
397
        if (id > 255)
398
        {
399
            System.err.println("Warning: No more ids are available for this module type: " + inputModule.getName());
400
        }
401
 
402
        return id;
403
    }
404
 
405
    public void addModule(Module module) throws Exception
406
    {
407
        myXML elementClasses = this.rootXML.findElement("root").findElement("classes");
408
 
409
        for (int n = 0; n < elementClasses.size(); n++)
410
        {
411
            myXML elementClass = elementClasses.getElement(n);
412
 
413
            if (elementClass.Attribute.find("name").compareTo(module.getCanClass().getName()) == 0)
414
            {
415
                myXML elementModules = elementClass.findElement("modules");
416
                if (elementModules == null)
417
                {
418
                    elementModules = elementClass.addElement("modules");
419
                }
420
 
421
                myXML elementModule = elementModules.addElement("module");
422
                elementModule.Attribute.add("id", module.getId());
423
                elementModule.Attribute.add("name", module.getName());
424
                elementModule.Attribute.add("description", module.getDescription());
425
 
426
                myXML elementSupportedCommands = elementModule.addElement("supportedCommands");
427
 
428
                ArrayList<SupportedCommand> supportedCommands = module.getSupportedCommands();
429
 
430
                Iterator<SupportedCommand> iterator = supportedCommands.iterator();
431
 
432
                while (iterator.hasNext())
433
                {
434
                    SupportedCommand supportedCommand = iterator.next();
435
 
436
                    myXML elementSupportedCommand = elementSupportedCommands.addElement("supportedCommand");
437
                    elementSupportedCommand.Attribute.add("name", supportedCommand.getName());
438
                    elementSupportedCommand.Attribute.add("direction", supportedCommand.getDirection().getName());
439
 
440
                    myXML elementSendingTypes = elementModule.addElement("sendingTypes");
441
 
442
                    ArrayList<SendingType> sendingTypes = supportedCommand.getSendingTypes();
443
                    Iterator<SendingType> iterator2 = sendingTypes.iterator();
444
 
445
                    while (iterator2.hasNext())
446
                    {
447
                        SendingType sendingType = iterator2.next();
448
 
449
                        myXML elementSendingtype = elementSendingTypes.addElement("sendingType");
450
                        elementSendingtype.Attribute.add("name", sendingType.getName());
451
                    }  
452
                }
453
 
454
                break;
455
            }
456
        }
457
 
458
        this.hasChanged = true;
459
        this.changeLog += "Added new module: " + module.getName() + " with id " + module.getId() + "\n";
460
    }
461
 
462
    /* ========================== */
463
    /* == Command type methods == */
464
    /* ========================== */
465
 
466
    public CommandType getCommandType(String name)
467
    {
468
        ArrayList<CommandType> commandTypes = this.getCommandTypes();
469
 
470
        Iterator<CommandType> iterator = commandTypes.iterator();
471
 
472
        while (iterator.hasNext())
473
        {
474
            CommandType commandType = iterator.next();
475
 
476
            if (commandType.getName().compareTo(name) == 0)
477
            {
478
                return commandType;
479
            }
480
        }
481
 
482
        return null;
483
    }
484
 
485
    public ArrayList<CommandType> getCommandTypes()
486
    {
487
        ArrayList<CommandType> commandTypes = new ArrayList<CommandType>();
488
 
489
        commandTypes.add(new CommandType("management", "", "NMT", 0, 255));
490
        commandTypes.add(new CommandType("global", "", "GLOB", 0, 63));
491
        commandTypes.add(new CommandType("physical", "", "PHYS", 64, 127));
492
        commandTypes.add(new CommandType("module", "", "MOD", 128, 191));
493
 
494
        return commandTypes;
495
    }
496
 
497
    /* ========================== */
498
    /* == Sending type methods == */
499
    /* ========================== */
500
 
501
    public SendingType getSendingType(String name)
502
    {
503
        ArrayList<SendingType> sendingTypes = this.getSendingTypes();
504
 
505
        Iterator<SendingType> iterator = sendingTypes.iterator();
506
 
507
        while (iterator.hasNext())
508
        {
509
            SendingType sendingType = iterator.next();
510
 
511
            if (sendingType.getName().compareTo(name) == 0)
512
            {
513
                return sendingType;
514
            }
515
        }
516
 
517
        return null;
518
    }
519
 
520
    public ArrayList<SendingType> getSendingTypes()
521
    {
522
        ArrayList<SendingType> sendingTypes = new ArrayList<SendingType>();
523
 
524
        sendingTypes.add(new SendingType("request", ""));
525
        sendingTypes.add(new SendingType("event", ""));
526
        sendingTypes.add(new SendingType("interval", ""));
527
 
528
        return sendingTypes;
529
    }
530
 
531
    /* ============================ */
532
    /* == Direction type methods == */
533
    /* ============================ */
534
 
535
    public DirectionType getDirectionType(String name)
536
    {
537
        ArrayList<DirectionType> directionTypes = this.getDirectionTypes();
538
 
539
        Iterator<DirectionType> iterator = directionTypes.iterator();
540
 
541
        while (iterator.hasNext())
542
        {
543
            DirectionType directionType = iterator.next();
544
 
545
            if (directionType.getName().compareTo(name) == 0)
546
            {
547
                return directionType;
548
            }
549
        }
550
 
551
        return null;
552
    }
553
 
554
    public ArrayList<DirectionType> getDirectionTypes()
555
    {
556
        ArrayList<DirectionType> directionTypes = new ArrayList<DirectionType>();
557
 
558
        directionTypes.add(new DirectionType("request", ""));
559
        directionTypes.add(new DirectionType("event", ""));
560
        directionTypes.add(new DirectionType("interval", ""));
561
 
562
        return directionTypes;
563
    }
564
}