summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1-RC4/demos/alert-aggregator-webapp/src/main/java/org/apache/tuscany/sca/demos/aggregator/AlertsSourcesServiceImpl.java
blob: ca2fc95b4a8b6824ad050a356ba3858a6ff518eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.sca.demos.aggregator;

import java.io.InputStream;
import java.text.DateFormat;
import java.util.Date;

import org.apache.tuscany.sca.demos.aggregator.types.ConfigType;
import org.apache.tuscany.sca.demos.aggregator.types.SourceType;
import org.apache.tuscany.sca.demos.aggregator.types.TypesFactory;
import org.apache.tuscany.sca.demos.aggregator.types.impl.SourceTypeImpl;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;

import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XMLDocument;
import commonj.sdo.impl.HelperProvider;

/**
 * Retrieve and manage alert sources
 *
 * @version $Rev$ $Date$
 */
@Service(AlertsSourcesService.class)
@Scope("COMPOSITE")
public class AlertsSourcesServiceImpl implements AlertsSourcesService {
    
    ConfigType alertSources;
    
    /**
     * Constructor reads the configuration to provide
     * the initial list of alert sources
     */
    public AlertsSourcesServiceImpl(){
        System.err.println("AlertsSourcesServiceImpl()");
        try {
            // read the alerts config from an XML file
            InputStream is = this.getClass().getClassLoader().getResourceAsStream("sources.xml");
            
            if (is == null) {
                throw new Exception("Can;t find sources.xml");
            } else {
                HelperContext helperContext = HelperProvider.getDefaultContext();
                TypesFactory.INSTANCE.register(helperContext);
                 XMLDocument xmlDoc = helperContext.getXMLHelper().load(is);
                alertSources = (ConfigType)xmlDoc.getRootObject();
            }
        } catch (Exception ex) {
            System.err.println("Exception " + ex.toString());
        }
    }
   
    /**
     * Return all of the configured alert sources.
     *
     * @return the list of alert sources
     */
    public ConfigType getAlertSources (String id)
    {           
        System.err.println("getAlertSources(" + id + ")");
        
        // convert alert sources to POJOs so that 
        // the JSONRPC binding will work
        ConfigTypeNonSDOImpl sources = new ConfigTypeNonSDOImpl();
        
        for (Object source : alertSources.getSource()) {
            SourceTypeNonSDOImpl newSource = new SourceTypeNonSDOImpl();
            newSource.setAddress(((SourceTypeImpl)source).getAddress());
            newSource.setFeedAddress(((SourceTypeImpl)source).getFeedAddress());
            newSource.setId(((SourceTypeImpl)source).getId());
            newSource.setLastChecked(((SourceTypeImpl)source).getLastChecked());
            newSource.setName(((SourceTypeImpl)source).getName());
            newSource.setFeedType(((SourceTypeImpl)source).getFeedType());
            sources.getSource().add(newSource);
        }
        
        return sources;
    }
    
    /**
     * Return a single alert source.
     * @param id the alert source id number
     * @return the alert source
     */
    public SourceType getAlertSource (String id)
    {           
        System.err.println("getAlertSource(" + id + ")");
        SourceType alertSource = null;
        
        for (Object source : alertSources.getSource()) {
            if ( ((SourceType)source).getId().equals(id)) {
                alertSource = (SourceType)source;
            }
        }
        return alertSource;
    } 
    
    /**
     * Update an alert source.
     *
     * @param updatedSource the alert source to update
     */
    public void updateAlertSource (SourceType updatedSource)
    {           
        System.err.println("updateAlertSource()");
            
        Object originalSource = null;
        
        for (Object source : alertSources.getSource()) {
            if ( ((SourceType)source).getId().equals(updatedSource.getId())) {
                originalSource = source;
                break;
            }
        }
        
        if (originalSource != null){
            alertSources.getSource().add(updatedSource);
            alertSources.getSource().remove(originalSource);
        }
    }    
      
    /**
     * Add an alert source.
     *
     * @param newSource the alert source to add
     */
    public String addAlertSource (SourceType newSource)
    {           
        System.err.println("addAlertSource()");
        // set the date to now less 2 hours so we 
        // get some alerts straight away
        DateFormat dateFormatter = DateFormat.getDateTimeInstance();
        Date now = new Date();
        now.setHours(now.getHours()-2);
        String nowString = dateFormatter.format(now);        
        newSource.setLastChecked(nowString); 
        alertSources.getSource().add(newSource);  
        return "Done";
    }    

    /**
     * Remove an alert source.
     *
     * @param oldSource the alert source to remove
     */
    public void removeAlertSource (String id)
    {           
        System.err.println("removeAlertSource()");
        
        Object originalSource = null;
        
        for (Object source : alertSources.getSource()) {
            if ( ((SourceType)source).getId().equals(id)) {
                originalSource = source;
                break;
            }
        }  
        
        if (originalSource != null) {
            alertSources.getSource().remove(originalSource);
        }

    } 
 
}