[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / model / DmaapObject.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import io.swagger.annotations.ApiModelProperty;
24 import java.util.Calendar;
25 import java.util.Date;
26 import java.util.TimeZone;
27 import javax.xml.bind.annotation.XmlRootElement;
28 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
29
30 @XmlRootElement
31 public abstract class DmaapObject extends BaseLoggingClass {
32         @ApiModelProperty( value = "datestamp for last update to this object")
33         protected Date lastMod;
34         protected       DmaapObject_Status      status;
35         
36         public Date getLastMod() {
37                 return lastMod;
38         }
39
40         public void setLastMod(Date lastMod) {
41                 this.lastMod = lastMod;
42         }
43
44         public void setLastMod() {
45                 this.lastMod = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
46         }
47         
48         public enum DmaapObject_Status {
49                 EMPTY,
50                 NEW,
51                 STAGED,
52                 VALID,
53                 INVALID,
54                 DELETED
55         }
56         public DmaapObject_Status getStatus() {
57                 return status;
58         }
59
60         public void setStatus(DmaapObject_Status status) {
61                 this.status = status;
62         }
63         
64         public void setStatus( String val ) {
65                 if ( val == null || val.isEmpty() ) {
66                         this.status = DmaapObject_Status.EMPTY;
67                 } else if (val.compareToIgnoreCase("new") == 0 ) {
68                         this.status = DmaapObject_Status.NEW;
69                 } else if ( val.compareToIgnoreCase("staged" ) == 0) {
70                         this.status = DmaapObject_Status.STAGED;
71                 } else if ( val.compareToIgnoreCase("valid") == 0) {
72                         this.status = DmaapObject_Status.VALID;
73                 } else if ( val.compareToIgnoreCase("invalid") == 0) {
74                         this.status = DmaapObject_Status.INVALID;
75                 } else if ( val.compareToIgnoreCase("deleted") == 0) {
76                         this.status = DmaapObject_Status.DELETED;
77                 } else {
78                         this.status = DmaapObject_Status.INVALID;
79                 }
80         }
81         
82         @ApiModelProperty( hidden=true )
83         public boolean isStatusValid() {
84                 if ( this.status == DmaapObject_Status.VALID ) {
85                         return true;
86                 }
87                 return false;
88         }
89         
90         /*
91          * TODO: get this working so arrays and sub-class within an Object can be logged
92          * 
93         public String toString() {
94                         return classToString( this );
95         }
96         
97         private String classToString( Object obj ) {
98                 Field[] fields = obj.getClass().getDeclaredFields();
99                 StringBuilder res = new StringBuilder( "{");
100                 boolean first = true;
101                 for ( Field field: fields ) {
102                         logger.info( field.getName() + " toString=" + field.toString() + " toGenericString=" + field.toGenericString());
103                         if ( first ) {
104                                 first = false;
105                         } else {
106                                 res.append( ", ");
107                         }
108
109
110                         field.setAccessible(true);  // avoid IllegalAccessException
111
112                         
113                         Class<?> t = field.getType();
114                         
115                         if ( t == String.class ) {
116                                 res.append( "\"" ).append( field.getName() ).append( "\": \"");
117         
118                                 try {
119                                         res.append(field.get(this));
120                                 } catch ( IllegalAccessException iae) {
121                                         res.append( "UNK(iae)");
122                                 } catch (IllegalArgumentException iae2 ) {
123                                         res.append( "UNK(iae2)");
124                                 } catch ( NullPointerException npe ) {
125                                         res.append( "UNK(npe)");
126                                 } catch ( ExceptionInInitializerError eie ) {
127                                         res.append( "UNK(eie)");
128                                 }
129                                 res.append( "\"");
130                         } else if ( t == ArrayList.class ){
131                                 res.append( "[");
132                                 res.append( classToString( field ));
133                                 res.append( "]");
134                                 
135                         }
136                 }
137                 res.append( "}");
138                 return( res.toString());
139         
140                 
141         }
142         */
143         
144 }