Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5b8520e62a2b0c605ce63bd3fc7ff105f2e6bc98 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * 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.server.admin.conversion;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.IResourceLocator;
import org.eclipse.osee.framework.resource.management.IResourceLocatorManager;
import org.eclipse.osee.framework.resource.management.IResourceManager;
import org.eclipse.osee.framework.resource.management.Options;
import org.eclipse.osee.framework.resource.management.StandardOptions;
import org.eclipse.osee.framework.resource.management.util.Resources;
import org.eclipse.osee.framework.server.admin.internal.Activator;
import org.eclipse.osgi.framework.console.CommandInterpreter;

/**
 * @author Roberto E. Escobar
 */
public class CompressedContentFix {

   private static CompressedContentFix instance = null;

   private static final String FIND_ALL_NATIVE_CONTENT_SQL =
      "SELECT art1.art_id, art1.human_readable_id, art1.guid, attr1.uri FROM osee_attribute attr1, osee_attribute_type attyp1, osee_artifact art1 WHERE attyp1.NAME = 'Native Content' AND attyp1.attr_type_id = attr1.attr_type_id AND art1.ART_ID = attr1.ART_ID";

   public static CompressedContentFix getInstance() {
      if (instance == null) {
         instance = new CompressedContentFix();
      }
      return instance;
   }

   private volatile boolean execute;
   private volatile boolean isRunning = false;
   private volatile boolean verbose = true;

   private CompressedContentFix() {
   }

   public void executeStop(CommandInterpreter ci) {
      execute = false;
   }

   public void execute(CommandInterpreter ci) {
      if (!isRunning) {
         execute = true;
         isRunning = true;
         Thread th = new Thread(new Worker(ci));
         th.setName("CompressedContentFix");
         th.start();
      }
   }

   private class Worker implements Runnable {

      private final CommandInterpreter ci;
      private final IResourceManager resourceManager;
      private final IResourceLocatorManager locatorManager;
      private Map<Long, String> nativeExtension;
      private Map<Long, String> nameMap;

      private Worker(CommandInterpreter cmdi) {
         ci = cmdi;
         resourceManager = Activator.getInstance().getResourceManager();
         locatorManager = Activator.getInstance().getResourceLocatorManager();
         nativeExtension = null;
         nameMap = null;
      }

      @Override
      public void run() {
         long time = System.currentTimeMillis();
         try {
            initializeData();
            doWork(time);
         } catch (OseeCoreException ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         } finally {
            clear();
         }
         long seconds = (System.currentTimeMillis() - time) / 1000;
         long leftOverSeconds = seconds % 60;
         long minutes = seconds / 60;
         long leftOverMinutes = minutes % 60;
         long hours = minutes / 60;
         ci.println(String.format("Done.  Elapsed Time = %d:%02d:%02d.", hours, leftOverMinutes, leftOverSeconds));
         isRunning = false;
      }

      private void initializeData() throws OseeCoreException {
         nativeExtension = Util.getArtIdMap("Extension");
         nameMap = Util.getArtIdMap("Name");
      }

      private void clear() {
         if (nativeExtension != null) {
            nativeExtension.clear();
            nativeExtension = null;
         }

         if (nameMap != null) {
            nameMap.clear();
            nameMap = null;
         }
      }

      private void processEntry(long artId, String hrid, String guid, String uri) throws Exception {
         boolean resourceExists = isResourceAvailable(uri);
         if (resourceExists) {
            IResourceLocator locator = locatorManager.getResourceLocator(uri);
            Options options = new Options();
            IResource resource = resourceManager.acquire(locator, options);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            String oldEntryName = getContent(resource, outputStream);
            String extension = nativeExtension.get(artId);
            String newEntryName = generateFileName(nameMap.get(artId), hrid, extension);

            byte[] compressed = Lib.compressStream(new ByteArrayInputStream(outputStream.toByteArray()), newEntryName);
            IResource modifiedResource =
               Resources.createResourceFromBytes(compressed, locator.getLocation().toASCIIString(), true);
            options.put(StandardOptions.Overwrite.name(), true);
            resourceManager.save(locator, modifiedResource, options);

            ci.println(String.format("hrid [%s] uri [%s] fileName[%s] newName[%s] extension[%s]", hrid, uri,
               oldEntryName, newEntryName, extension));
         }
      }

      public String generateFileName(String name, String hrid, String fileTypeExtension) {
         StringBuilder builder = new StringBuilder();
         if (name != null && name.length() > 0) {
            try {
               if (name.length() > 60) {
                  name = name.substring(0, 60);
               }
               builder.append(URLEncoder.encode(name, "UTF-8"));
               builder.append(".");
            } catch (Exception ex) {
               // Do Nothing - this is not important
            }
         }
         builder.append(hrid);

         if (fileTypeExtension != null && fileTypeExtension.length() > 0) {
            builder.append(".");
            builder.append(fileTypeExtension);
         }
         return builder.toString();
      }

      private String getContent(IResource resource, OutputStream outputStream) throws OseeCoreException {
         String name = null;
         InputStream inputStream = null;
         try {
            inputStream = resource.getContent();
            name = Lib.decompressStream(inputStream, outputStream);
         } catch (IOException ex) {
            OseeExceptions.wrapAndThrow(ex);
         } finally {
            Lib.close(inputStream);
         }
         return name;
      }

      private boolean isResourceAvailable(String uri) throws Exception {
         boolean resourceExists = false;
         if (uri != null && uri.length() > 0) {
            IResourceLocator locator = locatorManager.getResourceLocator(uri);
            resourceExists = resourceManager.exists(locator);
         }
         return resourceExists;
      }

      private void doWork(long time) {
         IOseeStatement chStmt = null;
         try {
            chStmt = ConnectionHandler.getStatement();
            chStmt.runPreparedQuery(FIND_ALL_NATIVE_CONTENT_SQL);
            int count = 0;
            while (chStmt.next() && execute) {
               long artId = chStmt.getLong("art_id");
               String hrid = chStmt.getString("human_readable_id");
               String guid = chStmt.getString("guid");
               String uri = chStmt.getString("uri");
               processEntry(artId, hrid, guid, uri);

               count++;
               if (count % 100 == 0) {
                  if (verbose) {
                     long seconds = (System.currentTimeMillis() - time) / 1000;
                     long leftOverSeconds = seconds % 60;
                     long minutes = seconds / 60;
                     long leftOverMinutes = minutes % 60;
                     long hours = minutes / 60;
                     ci.println(String.format("%d files processed, Elapsed Time = %d:%02d:%02d.", count, hours,
                        leftOverMinutes, leftOverSeconds));
                  }
               }
            }
         } catch (Exception ex) {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         } finally {
            if (chStmt != null) {
               chStmt.close();
            }
         }
      }
   }
}

Back to the top