2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.model;
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;
29 import javax.xml.bind.annotation.XmlRootElement;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
33 import io.swagger.annotations.ApiModelProperty;
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;
41 public Date getLastMod() {
45 public void setLastMod(Date lastMod) {
46 this.lastMod = lastMod;
49 public void setLastMod() {
50 this.lastMod = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
53 public enum DmaapObject_Status {
61 public DmaapObject_Status getStatus() {
65 public void setStatus(DmaapObject_Status status) {
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;
83 this.status = DmaapObject_Status.INVALID;
87 @ApiModelProperty( hidden=true )
88 public boolean isStatusValid() {
89 if ( this.status == DmaapObject_Status.VALID ) {
96 * TODO: get this working so arrays and sub-class within an Object can be logged
98 public String toString() {
99 return classToString( this );
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());
115 field.setAccessible(true); // avoid IllegalAccessException
118 Class<?> t = field.getType();
120 if ( t == String.class ) {
121 res.append( "\"" ).append( field.getName() ).append( "\": \"");
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)");
135 } else if ( t == ArrayList.class ){
137 res.append( classToString( field ));
143 return( res.toString());