DMAAP-83 Initial code import
[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 @XmlRootElement
34 public abstract class DmaapObject extends BaseLoggingClass {
35         protected Date lastMod;
36         protected       DmaapObject_Status      status;
37         
38         public Date getLastMod() {
39                 return lastMod;
40         }
41
42         public void setLastMod(Date lastMod) {
43                 this.lastMod = lastMod;
44         }
45
46         public void setLastMod() {
47                 this.lastMod = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
48         }
49         
50         public enum DmaapObject_Status {
51                 EMPTY,
52                 NEW,
53                 STAGED,
54                 VALID,
55                 INVALID,
56                 DELETED
57         }
58         public DmaapObject_Status getStatus() {
59                 return status;
60         }
61
62         public void setStatus(DmaapObject_Status status) {
63                 this.status = status;
64         }
65         
66         public boolean isStatusValid() {
67                 if ( this.status == DmaapObject_Status.VALID ) {
68                         return true;
69                 }
70                 return false;
71         }
72         
73         /*
74          * TODO: get this working so arrays and sub-class within an Object can be logged
75          * 
76         public String toString() {
77                         return classToString( this );
78         }
79         
80         private String classToString( Object obj ) {
81                 Field[] fields = obj.getClass().getDeclaredFields();
82                 StringBuilder res = new StringBuilder( "{");
83                 boolean first = true;
84                 for ( Field field: fields ) {
85                         logger.info( field.getName() + " toString=" + field.toString() + " toGenericString=" + field.toGenericString());
86                         if ( first ) {
87                                 first = false;
88                         } else {
89                                 res.append( ", ");
90                         }
91
92
93                         field.setAccessible(true);  // avoid IllegalAccessException
94
95                         
96                         Class<?> t = field.getType();
97                         
98                         if ( t == String.class ) {
99                                 res.append( "\"" ).append( field.getName() ).append( "\": \"");
100         
101                                 try {
102                                         res.append(field.get(this));
103                                 } catch ( IllegalAccessException iae) {
104                                         res.append( "UNK(iae)");
105                                 } catch (IllegalArgumentException iae2 ) {
106                                         res.append( "UNK(iae2)");
107                                 } catch ( NullPointerException npe ) {
108                                         res.append( "UNK(npe)");
109                                 } catch ( ExceptionInInitializerError eie ) {
110                                         res.append( "UNK(eie)");
111                                 }
112                                 res.append( "\"");
113                         } else if ( t == ArrayList.class ){
114                                 res.append( "[");
115                                 res.append( classToString( field ));
116                                 res.append( "]");
117                                 
118                         }
119                 }
120                 res.append( "}");
121                 return( res.toString());
122         
123                 
124         }
125         */
126         
127 }