Skip to main content
summaryrefslogtreecommitdiffstats
blob: 842ec33684a0159835963a27cc88b7598c728056 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.attribute.providers;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.skynet.core.attribute.utils.AbstractResourceProcessor;

/**
 * @author Roberto E. Escobar
 */
public class DataStore {
   private final AbstractResourceProcessor resourceProcessor;
   private String locator;
   private byte[] rawContent;
   private String contentType;
   private String encoding;
   private String extension;
   private boolean needToReadFromRemote;

   public DataStore(AbstractResourceProcessor resourceProcessor) {
      super();
      this.resourceProcessor = resourceProcessor;
      clear();
      this.needToReadFromRemote = true;
   }

   public String getLocator() {
      return this.locator;
   }

   public void setLocator(String locator) {
      this.locator = locator;
      needToReadFromRemote = true;
   }

   public boolean isLocatorValid() {
      return this.locator != null && this.locator.length() > 0;
   }

   public boolean isDataValid() {
      return this.rawContent != null && this.rawContent.length > 0;
   }

   public InputStream getInputStream() throws OseeCoreException {
      return new ByteArrayInputStream(getContent());
   }

   public byte[] getContent() throws OseeCoreException {
      if (isLocatorValid() != false && needToReadFromRemote) {
         resourceProcessor.acquire(this);
         needToReadFromRemote = false;
      }
      return this.rawContent;
   }

   public void setContent(byte[] rawContent, String extension, String contentType, String encoding) {
      this.rawContent = rawContent;
      this.contentType = contentType;
      this.encoding = encoding;
      this.extension = extension;
   }

   public void copyTo(DataStore other) {
      other.rawContent = Arrays.copyOf(this.rawContent, this.rawContent.length);
      other.contentType = this.contentType;
      other.encoding = this.encoding;
   }

   public void persist(int storageId) throws OseeCoreException {
      if (this.rawContent != null && this.rawContent.length > 0) {
         resourceProcessor.saveResource(storageId, resourceProcessor.createStorageName(), this);
      }
   }

   public void purge() throws OseeCoreException {
      if (isLocatorValid() != false) {
         resourceProcessor.purge(this);
      }
   }

   /**
    * @return the contentType
    */
   public String getContentType() {
      return contentType;
   }

   /**
    * @return the encoding
    */
   public String getEncoding() {
      return encoding;
   }

   public void clear() {
      setContent(null, "txt", "txt/plain", "UTF-8");
      setLocator(null);
   }

   /**
    * @return the extension
    */
   public String getExtension() {
      return extension;
   }
}

Back to the top