📄 cmsresourcetypefolder.java
字号:
String bodyFolder = C_CONTENTBODYPATH.substring(0,
C_CONTENTBODYPATH.lastIndexOf("/")) + folder.getAbsolutePath();
try {
cms.readFolder(bodyFolder);
cms.doChown(bodyFolder, newOwner);
}
catch(CmsException ex) {
// no folder is there, so do nothing
}
// the rekursive flag was set do a recursive chmod on all files and subfolders
if(chRekursive) {
// get all subfolders and files
Vector allFolders = new Vector();
Vector allFiles = new Vector();
getAllResources(cms, filename, allFiles, allFolders);
// now modify all subfolders
for(int i = 0;i < allFolders.size();i++) {
CmsFolder curfolder = (CmsFolder)allFolders.elementAt(i);
if(curfolder.getState() != C_STATE_DELETED) {
cms.doChown(curfolder.getAbsolutePath(), newOwner);
// check if there is a corresponding directory in the content body folder
bodyFolder = C_CONTENTBODYPATH.substring(0,
C_CONTENTBODYPATH.lastIndexOf("/")) + curfolder.getAbsolutePath();
try {
cms.readFolder(bodyFolder);
cms.doChown(bodyFolder, newOwner);
}catch(CmsException ex) {
// no folder is there, so do nothing
}
}
}
// now modify all files in the subfolders
for(int i = 0;i < allFiles.size();i++) {
CmsFile newfile = (CmsFile)allFiles.elementAt(i);
if(newfile.getState() != C_STATE_DELETED) {
try{
cms.chown(newfile.getAbsolutePath(), newOwner);
} catch (CmsException e){
if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
A_OpenCms.log(A_OpenCms.C_OPENCMS_CRITICAL, "["+this.getClass().getName()+"] "+newfile.getAbsolutePath()+": "+e.getStackTraceAsString());
}
}
}
}
}
}else{
throw new CmsException("[" + this.getClass().getName() + "] " + filename,
CmsException.C_NO_ACCESS);
}
}
/**
* Changes the resourcetype of a resource.
* <br>
* Only the resourcetype of a resource in an offline project can be changed. The state
* of the resource is set to CHANGED (1).
* If the content of this resource is not exisiting in the offline project already,
* it is read from the online project and written into the offline project.
* The user may change this, if he is admin of the resource.
* <p>
* <B>Security:</B>
* Access is granted, if:
* <ul>
* <li>the user has access to the project</li>
* <li>the user is owner of the resource or is admin</li>
* <li>the resource is locked by the callingUser</li>
* </ul>
*
* @param filename the complete path to the resource.
* @param newType the name of the new resourcetype for this resource.
*
* @exception CmsException if operation was not successful.
*/
public void chtype(CmsObject cms, String filename, String newType) throws CmsException{
// it is not possible to change the type of a folder
throw new CmsException("[" + this.getClass().getName() + "] " + filename,
CmsException.C_ACCESS_DENIED);
}
/**
* Copies a Resource.
*
* @param source the complete path of the sourcefile.
* @param destination the complete path of the destinationfolder.
* @param keepFlags <code>true</code> if the copy should keep the source file's flags,
* <code>false</code> if the copy should get the user's default flags.
*
* @exception CmsException if the file couldn't be copied, or the user
* has not the appropriate rights to copy the file.
*/
public void copyResource(CmsObject cms, String source, String destination, boolean keepFlags) throws CmsException{
// we have to copy the folder and all resources in the folder
Vector allSubFolders = new Vector();
Vector allSubFiles = new Vector();
// first valid the destination name
validResourcename(destination);
getAllResources(cms, source, allSubFiles, allSubFolders);
if (!destination.endsWith("/")){
destination = destination +"/";
}
if (!destination.startsWith("/")){
destination = "/"+destination ;
}
// first the folder
cms.doCopyFolder(source, destination);
if(!keepFlags){
setDefaultFlags(cms, destination);
}
// now the subfolders
for (int i=0; i<allSubFolders.size(); i++){
CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
if(curFolder.getState() != C_STATE_DELETED){
String curDestination = destination + curFolder.getAbsolutePath().substring(source.length());
cms.doCopyFolder(curFolder.getAbsolutePath(), curDestination );
if(!keepFlags){
setDefaultFlags(cms, curDestination);
}
}
}
// now all the little files
for (int i=0; i<allSubFiles.size(); i++){
CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
if(curFile.getState() != C_STATE_DELETED){
String curDest = destination + curFile.getAbsolutePath().substring(source.length());
cms.copyResource(curFile.getAbsolutePath(), curDest, keepFlags);
}
}
// copy the content bodys
try{
copyResource(cms, C_CONTENTBODYPATH + source.substring(1), C_CONTENTBODYPATH + destination.substring(1), keepFlags);
// finaly lock the copy in content bodys if it exists.
cms.lockResource(C_CONTENTBODYPATH + destination.substring(1));
}catch(CmsException e){
}
}
/**
* Copies a resource from the online project to a new, specified project.
* <br>
* Copying a resource will copy the file header or folder into the specified
* offline project and set its state to UNCHANGED.
*
* @param resource the name of the resource.
* @exception CmsException if operation was not successful.
*/
public void copyResourceToProject(CmsObject cms, String resourceName) throws CmsException {
// copy the folder to the current project
cms.doCopyResourceToProject(resourceName);
// try to copy the corresponding folder in /content/bodys/ to the project
try{
CmsResource contentFolder = (CmsResource)cms.readFolder(C_CONTENTBODYPATH.substring(0, C_CONTENTBODYPATH.lastIndexOf("/"))+resourceName, true);
if (contentFolder != null){
cms.doCopyResourceToProject(contentFolder.getAbsolutePath());
}
} catch(CmsException e){
// cannot read the folder in /content/bodys/ so do nothing
}
}
/**
* Creates a new resource.<br>
*
* @param folder the complete path to the folder in which the file will be created.
* @param filename the name of the new resource.
* @param contents the contents of the new file.
* @param type the resourcetype of the new file.
*
* @return file a <code>CmsFile</code> object representing the newly created file.
*
* @exception CmsException or if the resourcetype is set to folder. The CmsException is also thrown, if the
* filename is not valid or if the user has not the appropriate rights to create a new file.
*/
public CmsResource createResource(CmsObject cms, String folder, String name, Hashtable properties, byte[] contents) throws CmsException{
CmsFolder res = cms.doCreateFolder(folder, name, properties);
cms.lockResource(folder+name+"/");
res.setLocked(cms.getRequestContext().currentUser().getId());
return res;
}
/**
* Deletes a resource.
*
* @param folder the complete path of the folder.
*
* @exception CmsException if the file couldn't be deleted, or if the user
* has not the appropriate rights to delete the file.
*/
public void deleteResource(CmsObject cms, String folder) throws CmsException{
Vector allSubFolders = new Vector();
Vector allSubFiles = new Vector();
getAllResources(cms, folder, allSubFiles, allSubFolders);
// first delete all the files
for (int i=0; i<allSubFiles.size(); i++){
CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
if(curFile.getState() != C_STATE_DELETED){
try{
cms.deleteResource(curFile.getAbsolutePath());
}catch(CmsException e){
if(e.getType() != CmsException.C_RESOURCE_DELETED){
throw e;
}
}
}
}
// now all the empty subfolders
for (int i=allSubFolders.size() - 1; i > -1; i--){
CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
if(curFolder.getState() != C_STATE_DELETED){
cms.doDeleteFolder(curFolder.getAbsolutePath());
}
}
// finaly the folder
cms.doDeleteFolder(folder);
// delete the corresponding folder in /content/bodys/
String bodyFolder = C_CONTENTBODYPATH.substring(0,
C_CONTENTBODYPATH.lastIndexOf("/")) + folder;
try {
cms.readFolder(bodyFolder);
cms.deleteResource(bodyFolder);
}
catch(CmsException ex) {
// no folder is there, so do nothing
}
}
/**
* Undeletes a resource.
*
* @param folder the complete path of the folder.
*
* @exception CmsException if the file couldn't be undeleted, or if the user
* has not the appropriate rights to undelete the file.
*/
public void undeleteResource(CmsObject cms, String folder) throws CmsException{
// we have to undelete the folder and all resources in the folder
Vector allSubFolders = new Vector();
Vector allSubFiles = new Vector();
getAllResources(cms, folder, allSubFiles, allSubFolders);
// first undelete all the files
for (int i=0; i<allSubFiles.size(); i++){
CmsFile curFile = (CmsFile)allSubFiles.elementAt(i);
if(curFile.getState() == C_STATE_DELETED){
cms.undeleteResource(curFile.getAbsolutePath());
}
}
// now all the empty subfolders
for (int i=0; i<allSubFolders.size(); i++){
CmsFolder curFolder = (CmsFolder) allSubFolders.elementAt(i);
if(curFolder.getState() == C_STATE_DELETED){
cms.doUndeleteFolder(curFolder.getAbsolutePath());
}
}
// finally the folder
cms.doUndeleteFolder(folder);
// undelete the corresponding folder in /content/bodys/
String bodyFolder = C_CONTENTBODYPATH.substring(0,
C_CONTENTBODYPATH.lastIndexOf("/")) + folder;
try {
cms.readFolder(bodyFolder,true);
cms.undeleteResource(bodyFolder);
}
catch(CmsException ex) {
// no folder is there, so do nothing
}
}
/**
* Does the Linkmanagement when a resource will be exported.
* When a resource has to be exported, the ID磗 inside the
* Linkmanagement-Tags have to be changed to the corresponding URL磗
*
* @param file is the file that has to be changed
*/
public CmsFile exportResource(CmsObject cms, CmsFile file) throws CmsException {
// nothing to do here, because there couldn磘 be any Linkmanagement-Tags inside a folder-resource
return file;
}
/**
* Imports a Folder.
* Does the Linkmanagement when a resource is imported.
* When a resource has to be imported, the URL磗 of the
* Links inside the resources have to be saved and changed to the corresponding ID磗
*
* @param file is the file that has to be changed
*/
public CmsResource importResource(CmsObject cms, String source, String destination, String type, String user, String group, String access, Hashtable properties, String launcherStartClass, byte[] content, String importPath) throws CmsException {
CmsResource cmsfolder = null;
String path = importPath + destination.substring(0, destination.lastIndexOf("/") + 1);
String name = destination.substring((destination.lastIndexOf("/") + 1), destination.length());
String fullname = null;
try {
cmsfolder = createResource(cms, path, name, properties, content);
if(cmsfolder != null){
fullname = cmsfolder.getAbsolutePath();
lockResource(cms, fullname, true);
}
} catch (CmsException e) {
// an exception is thrown if the folder already exists
}
if(fullname == null){
//the folder exists, check if properties has to be updated
cmsfolder = cms.readFolder(path, name + "/");
Hashtable oldProperties = cms.readAllProperties(cmsfolder.getAbsolutePath());
if(oldProperties == null){
oldProperties = new Hashtable();
}
if(properties == null){
properties = new Hashtable();
}
if( !oldProperties.equals(properties)){
fullname = cmsfolder.getAbsolutePath();
lockResource(cms, fullname, true);
cms.writeProperties(fullname, properties);
}
if(fullname == null){
// properties are the same but what about the owner, group and access?
if(cmsfolder.getAccessFlags() != Integer.parseInt(access) ||
cmsfolder.getOwnerId() != cms.readUser(user).getId() ||
cmsfolder.getGroupId() != cms.readGroup(group).getId() ){
fullname = cmsfolder.getAbsolutePath();
lockResource(cms, fullname, true);
}
}
}
if(fullname != null){
try{
cms.chmod(fullname, Integer.parseInt(access));
}catch(CmsException e){
System.out.println("chmod(" + access + ") failed ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -