package pt.estgp.estgweb.services.data;
import jomm.utils.StreamsUtils;
import org.dom4j.Document;
import pt.estgp.estgweb.Globals;
import pt.estgp.estgweb.utils.Dom4jUtil;
import java.io.*;
/**
* @author Jorge Machado
* @date 28/Mai/2008
* @see pt.estgp.estgweb.services.data
*/
public class ResourceManager
{
public static final int DOCS_IN_PATH =
1000;
public static final int DOCS_IN_PATH_LEVEL1 = DOCS_IN_PATH
* DOCS_IN_PATH
;
public static final int DOCS_IN_PATH_LEVEL2 = DOCS_IN_PATH
* DOCS_IN_PATH
* DOCS_IN_PATH
;
public static final int DOCS_FIRST_DIR_LEN =
10;
public static void saveResource
(5+0%2Fdocs%2Fapi+InputStream">InputStream stream,
1.5.0/docs/api/java/lang/String.html">String resourcePath,
long id
) throws 1.5.0/docs/api/java/io/IOException.html">IOException
{
StreamsUtils.
inputStream2File(stream,
new 1.5.0/docs/api/java/io/File.html">File(Globals.
DATA_DIR + resourcePath + getDocumentPathById
(id
)));
}
public static void saveResource
(5+0%2Fdocs%2Fapi+Document">Document dom,
1.5.0/docs/api/java/lang/String.html">String resourcePath,
long id
) throws 1.5.0/docs/api/java/io/IOException.html">IOException
{
new 1.5.0/docs/api/java/io/File.html">File(Globals.
DATA_DIR + resourcePath + getDocumentPathById
(id
)+
".xml").
getParentFile().
mkdirs();
Dom4jUtil.
write(dom,Globals.
DATA_DIR + resourcePath + getDocumentPathById
(id
)+
".xml");
}
public static 5+0%2Fdocs%2Fapi+InputStream">InputStream getResource
(1.5.0/docs/api/java/lang/String.html">String resourcePath,
long id
) throws 1.5.0/docs/api/java/io/FileNotFoundException.html">FileNotFoundException
{
return new 1.5.0/docs/api/java/io/FileInputStream.html">FileInputStream(Globals.
DATA_DIR + resourcePath + getDocumentPathById
(id
));
}
public static void setAllChars
(char c,
char[] dir
)
{
for (int i =
0; i
< dir.
length; i++
)
{
dir
[i
] = c
;
}
}
public static void copyCharsToArray
(char[] dir,
1.5.0/docs/api/java/lang/String.html">String str
)
{
int dirLen = dir.
length;
int strLen = str.
length();
for (int i =
0; i
< strLen
; i++
)
{
dir
[dirLen - i - 1
] = str.
charAt(strLen - i - 1
);
}
}
public static 1.5.0/docs/api/java/lang/String.html">String getDocumentPathById
(long id
)
{
char[] dir0 =
new char[DOCS_FIRST_DIR_LEN
];
setAllChars
('0', dir0
);
char[] dir1 =
new char[DOCS_FIRST_DIR_LEN
];
setAllChars
('0', dir1
);
char[] dir2 =
new char[DOCS_FIRST_DIR_LEN
];
setAllChars
('0', dir2
);
long dirNumber0 = id / DOCS_IN_PATH_LEVEL2
;
long dirNumber1 = id / DOCS_IN_PATH_LEVEL1
;
long dirNumber2 = id / DOCS_IN_PATH
;
dirNumber0 = dirNumber0
* DOCS_IN_PATH_LEVEL2
;
dirNumber1 = dirNumber1
* DOCS_IN_PATH_LEVEL1
;
dirNumber2 = dirNumber2
* DOCS_IN_PATH
;
1.5.0/docs/api/java/lang/String.html">String dirNumberStr0 =
"" + dirNumber0
;
1.5.0/docs/api/java/lang/String.html">String dirNumberStr1 =
"" + dirNumber1
;
1.5.0/docs/api/java/lang/String.html">String dirNumberStr2 =
"" + dirNumber2
;
copyCharsToArray
(dir0, dirNumberStr0
);
copyCharsToArray
(dir1, dirNumberStr1
);
copyCharsToArray
(dir2, dirNumberStr2
);
dirNumberStr0 =
new 1.5.0/docs/api/java/lang/String.html">String(dir0
);
dirNumberStr1 =
new 1.5.0/docs/api/java/lang/String.html">String(dir1
);
dirNumberStr2 =
new 1.5.0/docs/api/java/lang/String.html">String(dir2
);
return "/" + dirNumberStr0 +
"/" + dirNumberStr1 +
"/" + dirNumberStr2 +
"/" + id
;
}
}