Release patch 2.0.4
[dmaap/dbcapi.git] / 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 java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.TimeZone;
28
29 import javax.xml.bind.annotation.XmlRootElement;
30
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
32
33 import io.swagger.annotations.ApiModelProperty;
34
35 @XmlRootElement
36 public abstract class DmaapObject extends BaseLoggingClass {
37         @ApiModelProperty( value = "datestamp for last update to this object")
38         protected Date lastMod;
39         protected       DmaapObject_Status      status;
40         
41         public Date getLastMod() {
42                 return lastMod;
43         }
44
45         public void setLastMod(Date lastMod) {
46                 this.lastMod = lastMod;
47         }
48
49         public void setLastMod() {
50                 this.lastMod = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
51         }
52         
53         public enum DmaapObject_Status {
54                 EMPTY,
55                 NEW,
56                 STAGED,
57                 VALID,
58                 INVALID,
59                 DELETED
60         }
61         public DmaapObject_Status getStatus() {
62                 return status;
63         }
64
65         public void setStatus(DmaapObject_Status status) {
66                 this.status = status;
67         }
68         
69         public void setStatus( String val ) {
70                 if ( val == null || val.isEmpty() ) {
71                         this.status = DmaapObject_Status.EMPTY;
72                 } else if (val.compareToIgnoreCase("new") == 0 ) {
73                         this.status = DmaapObject_Status.NEW;
74                 } else if ( val.compareToIgnoreCase("staged" ) == 0) {
75                         this.status = DmaapObject_Status.STAGED;
76                 } else if ( val.compareToIgnoreCase("valid") == 0) {
77                         this.status = DmaapObject_Status.VALID;
78                 } else if ( val.compareToIgnoreCase("invalid") == 0) {
79                         this.status = DmaapObject_Status.INVALID;
80                 } else if ( val.compareToIgnoreCase("deleted") == 0) {
81                         this.status = DmaapObject_Status.DELETED;
82                 } else {
83                         this.status = DmaapObject_Status.INVALID;
84                 }
85         }
86         
87         @ApiModelProperty( hidden=true )
88         public boolean isStatusValid() {
89                 if ( this.status == DmaapObject_Status.VALID ) {
90                         return true;
91                 }
92                 return false;
93         }
94         
95         /*
96          * TODO: get this working so arrays and sub-class within an Object can be logged
97          * 
98         public String toString() {
99                         return classToString( this );
100         }
101         
102         private String classToString( Object obj ) {
103                 Field[] fields = obj.getClass().getDeclaredFields();
104                 StringBuilder res = new StringBuilder( "{");
105                 boolean first = true;
106                 for ( Field field: fields ) {
107                         logger.info( field.getName() + " toString=" + field.toString() + " toGenericString=" + field.toGenericString());
108                         if ( first ) {
109                                 first = false;
110                         } else {
111                                 res.append( ", ");
112                         }
113
114
115                         field.setAccessible(true);  // avoid IllegalAccessException
116
117                         
118                         Class<?> t = field.getType();
119                         
120                         if ( t == String.class ) {
121                                 res.append( "\"" ).append( field.getName() ).append( "\": \"");
122         
123                                 try {
124                                         res.append(field.get(this));
125                                 } catch ( IllegalAccessException iae) {
126                                         res.append( "UNK(iae)");
127                                 } catch (IllegalArgumentException iae2 ) {
128                                         res.append( "UNK(iae2)");
129                                 } catch ( NullPointerException npe ) {
130                                         res.append( "UNK(npe)");
131                                 } catch ( ExceptionInInitializerError eie ) {
132                                         res.append( "UNK(eie)");
133                                 }
134                                 res.append( "\"");
135                         } else if ( t == ArrayList.class ){
136                                 res.append( "[");
137                                 res.append( classToString( field ));
138                                 res.append( "]");
139                                 
140                         }
141                 }
142                 res.append( "}");
143                 return( res.toString());
144         
145                 
146         }
147         */
148         
149 }