code: public static CmsLockActionRecord ensureLock(CmsObject cms, CmsResource resource) throws CmsException {
  
          LockChange change = LockChange.unchanged;
          List<CmsResource> blockingResources = cms.getBlockingLockedResources(resource);
          if ((blockingResources != null) && !blockingResources.isEmpty()) {
              throw new CmsException(
                  Messages.get().container(
                      Messages.ERR_RESOURCE_HAS_BLOCKING_LOCKED_CHILDREN_1,
                      cms.getSitePath(resource)));
          }
          CmsUser user = cms.getRequestContext().getCurrentUser();
          CmsLock lock = cms.getLock(resource);
          if (!lock.isOwnedBy(user)) {
              cms.lockResourceTemporary(resource);
              change = LockChange.locked;
              lock = cms.getLock(resource);
          } else if (!lock.isOwnedInProjectBy(user, cms.getRequestContext().getCurrentProject())) {
              cms.changeLock(resource);
              change = LockChange.changed;
              lock = cms.getLock(resource);
          }
          return new CmsLockActionRecord(lock, change);
      }
docs: Static helper method to lock a resource.<p>
  
  @param cms the CMS context to use
  @param resource the resource to lock
  @return the action that was taken
  
  @throws CmsException if something goes wrong